Reputation: 23
How would I label the points in this scatter plot using numbers instead of colors?
Below is the code I am using, instead of the legend saying what color is related to what change I would like it to use numbers. It's hard to tell what color it is since I am using colored panels.
Code:
d=data.frame(x1=c(.5,2,.5,2),
x2 = c(2,3.5,2,3.5),
y1 = c(.5,.5,2,2),
y2 = c(2,2,3.2,3.2),
t=c('low,low','high,low','low,high','high,high'),
r=c('low,low','high,low','low,high','high,high'))
ggplot() +
geom_point(data = df, aes(x=df$Impact, y=df$Likelihood, colour = df$Change)) +
scale_x_continuous(name = "Impact", limits = c(.5,3.5),
breaks=seq(.5,3.5, 1), labels = seq(.5,3.5, 1)) +
scale_y_continuous(name = "Likelihood", limits = c(.5,3.2),
breaks=seq(.5, 3.2, 1), labels = seq(.5, 3.2, 1)) +
geom_rect(data=d,
mapping = aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = t),
alpha = .5, color = "black")+
geom_text(data=d,
aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r),
size=4)
I would like each item i.e 'Add Server' to correspond to a unique integer and then for that integer to be plotted. Thanks
Edit:
Dataframe structure:
Columns: Change (string), Impact (float), Likelihood (float)
dput(df)
structure(list(Change = c("Windows Patches\n-CRPDB1", "Change DNS settings",
"SSIS Schedule change\n-Warehouse", "OnBase Upgrade", "Add Server",
"Change IL Parameter", "Code Change - Validation missing", "Mass Update Data in Infolease",
"User add, remove or update user permission", "ServiceNow Deployment",
"Creating of a sever or desktop image for mass deployment", "Database table update. Column add/modify",
"Update add PRTG/Sensor"), Impact = c(3, 1.8, 2.6, 2.3, 1, 2.25,
1.8, 1.95, 1.3, 1.5, 1.8, 1, 1), Likelihood = c(3, 1.75, 1.7,
1.6, 1.3, 1.15, 1.15, 1.15, 1.15, 1.1, 1, 1, 1)), class = "data.frame", row.names = c(NA,
-13L))
Upvotes: 1
Views: 1046
Reputation: 29085
You can keep the aesthetic mapping between change & colour in order to create a legend, while setting that layer invisible so that it doesn't detract from the overall picture:
df$ID <- seq(1, nrow(df))
df$Legend <- paste0(df$ID, ". ", df$Change)
df$Legend <- factor(df$Legend,
levels = df$Legend[order(df$ID)])
p <- ggplot() +
# text layer to position the numbers
geom_text(data = df,
aes(x = Impact, y = Likelihood, label = ID)) +
# invisible layer to create legend for the numbers
geom_point(data = df,
aes(x = Impact, y = Likelihood, colour = Legend),
alpha = 0, size = 0) +
# rest of the code is unchanged
scale_x_continuous(name = "Impact", limits = c(.5,3.5),
breaks=seq(.5,3.5, 1), labels = seq(.5,3.5, 1)) +
scale_y_continuous(name = "Likelihood", limits = c(.5,3.2),
breaks=seq(.5, 3.2, 1), labels = seq(.5, 3.2, 1)) +
geom_rect(data=d,
aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = t),
alpha = .5, color = "black") +
geom_text(data=d,
aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r),
size=4)
p
In addition, if you want to remove the empty grey legend keys, set its key width to 0:
p + scale_color_discrete(guide = guide_legend(keywidth = unit(0, "pt")))
Upvotes: 1
Reputation: 913
I cannot think of a way to do this using only ggplot2 functions, but maybe there is an elegant way to do so. Instead, you can use gridExtra and a tableGrob
to display the correct legend.
I replace your call to geom_point()
with a call to geom_text()
, convert to a grob, then create a table grob with the text you want displayed in the legend, and finally arrange the two grobs.
# load your data as d and df
library(grid)
library(gridExtra)
# add in a Label column with numbers
df$Label <- 1:nrow(df)
g2 <- ggplot() +
geom_text(data = df, aes(x = Impact, y = Likelihood, label = Label)) +
scale_x_continuous(
name = "Impact",
limits = c(.5,3.5),
breaks=seq(.5,3.5, 1),
labels = seq(.5,3.5, 1)
) +
scale_y_continuous(
name = "Likelihood",
limits = c(.5,3.2),
breaks=seq(.5, 3.2, 1),
labels = seq(.5, 3.2, 1)
) +
geom_rect(
data = d,
mapping = aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2, fill = t),
alpha = .5,
color = "black"
) +
geom_text(data = d, aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r), size=4)
g2_grob <- ggplotGrob(g2)
# pasted the two columns together for it to appear a little nicer
tab_leg <- tableGrob(
paste(df$Label,"-", df$Change),
theme = ttheme_minimal(
core = list(fg_params = list(hjust=0, x=0.1,fontsize=8))
)
)
# arrange the plot and table
grid.arrange(arrangeGrob(
g2_grob, nullGrob(), tab_leg, nullGrob(),
layout_matrix = matrix(1:4, ncol = 4),
widths = c(6,.5,2,1)
))
If you want to move the region legend around, you can check out this answer: Show the table of values under the bar plot.
Upvotes: 1