我有以下数据:
metric_list <- c(0.6, 0.2, 0.1, 0.05)
terms_used = c("a", "b", "c", "d")
我正在使用以下代码获取情节:
df <- data.frame(x = 1:length(metric_list),
terms_used = terms_used,
metric_list = 100*metric_list,
cumulative = cumsum(100*metric_list))
gplot <- ggplot(data = df, aes(x = x)) +
geom_point(aes(y = metric_list,
color = "ERR"),
size = 3) +
geom_text(aes(y = metric_list,
label = terms_used),
size = 6,
hjust = -1,
vjust = 0) +
geom_line(aes(y = cumulative,
color = "ESR",
linetype = "ESR"),
size = 1) +
geom_point(aes(y = cumulative,
color = "ESR",
shape = "ESR"),
size = 3) +
scale_colour_manual(name = "Legend",
values = c("ERR" = "blue", "ESR" = "red")) +
scale_linetype_manual(name = "Legend",
values = c("ERR" = 0, "ESR" = "dashed")) +
scale_shape_manual(name = "Legend",
values = c("ERR" = 1, "ESR" = 0))
我得到的是以下内容:
我想知道如何修改图例,以便只显示蓝点(对于ERR)和带有空心正方形(对于ESR)的红线。非常感谢任何帮助。
答案 0 :(得分:4)
ggplot2效果最佳。 (请参阅ggplot2的创建者Hadley Wickham的整洁数据presentation或paper。)因此,而不是:
> df
x terms_used metric_list cumulative
1 1 a 60 60
2 2 b 20 80
3 3 c 10 90
4 4 d 5 95
你应该使用
> DF
x terms_used var_name val
1 1 a ERR 60
2 2 b ERR 20
3 3 c ERR 10
4 4 d ERR 5
5 1 ESR 60
6 2 ESR 80
7 3 ESR 90
8 4 ESR 95
tidyr
包可用于重塑数据:
library(tidyr) # for gather
library(plyr) # for revalue
# Reshape from wide to long
DF <- gather(df, var_name, val, c(metric_list, cumulative))
# change levels of factor (e.g. metric_list to ERR)
DF$var_name <- revalue(DF$var_name,
c("metric_list"="ERR", "cumulative"="ESR"))
然后,您可以创建如下图:
# Remove letters beside red points on plot
DF$terms_used <- as.character(DF$terms_used)
DF$terms_used[DF$var_name=="ESR"] <- ""
gplot <- ggplot(data = DF,
aes(x=x, y=val, group=var_name, colour=var_name,
linetype=var_name, shape=var_name)) +
geom_point() +
geom_text(aes(label=terms_used),
size = 6,
hjust = -1,
vjust = 0,
show_guide = FALSE) +
geom_line(size = 1) +
geom_point(size = 3) +
scale_colour_manual(values = c("blue", "red")) +
scale_linetype_manual(values = c("blank", "dashed")) +
scale_shape_manual(values = c(1,0))
gplot