[英]ggplot2 Geom_Plot R labeling points in scatter plot
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 ie '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))
I cannot think of a way to do this using only ggplot2 functions, but maybe there is an elegant way to do so. 我无法想到仅使用ggplot2函数执行此操作的方法,但是也许有一种优雅的方法可以执行此操作。 Instead, you can use gridExtra and a tableGrob
to display the correct legend. 而是可以使用gridExtra和tableGrob
来显示正确的图例。
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. 我geom_point()
的调用替换为对geom_point()
的调用,转换为geom_text()
,然后使用要在图例中显示的文本创建表grob,最后排列两个grob。
# 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 . 如果要移动区域图例,可以查看以下答案: 在条形图下显示值表 。
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: 此外,如果要删除空白的灰色图例键,请将其键宽度设置为0:
p + scale_color_discrete(guide = guide_legend(keywidth = unit(0, "pt")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.