我正在尝试更改ggplot图的图例,但我没有这样做。这是我的df和图表的代码:
#df
df = structure(list(Date = structure(c(18319, 18320, 18321, 18322,
18323, 18324, 18325, 18326, 18327, 18328, 18329, 18330, 18331,
18332, 18333, 18334, 18335, 18336, 18337, 18338, 18339, 18340,
18341, 18342, 18343), class = "Date"), Daily_Cases = c(250, 238,
240, 566, 342, 466, 587, 769, 778, 1247, 1492, 1797, 977, 2313,
2651, 2547, 3497, 3590, 3233, 3526, 4207, 5322, 5986, 6557, 5560
), Tests = c(2427, 3681, 2966, 2466, 2218, 2511, 3981, 2525,
3997, 5703, 7875, 3889, 6935, 12393, 12857, 11477, 11682, 15729,
13063, 10695, 16884, 17236, 24109, 26336, 25180), Proportion = c(10.3,
6.5, 8.1, 23, 15.4, 18.6, 14.7, 30.5, 19.5, 21.9, 18.9, 46.2,
14.1, 18.7, 20.6, 22.2, 29.9, 22.8, 24.7, 33, 24.9, 30.9, 24.8,
24.9, 22.1)), class = "data.frame", row.names = c(NA, -25L))
df <- df %>% pivot_longer(names_to = 'Legend', values_to = 'value', Tests:Daily_Cases)
ggplot(df) + theme_bw() +
geom_col(aes(Date, value, fill = Legend), position = position_dodge()) +
geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) + scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") +
scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
theme(legend.position = "bottom")
结果如下:
不过,我希望能够删除单词联想,改变它的话Daily_Cases和测试到案件数和试验数分别。
我没有这样做。谁能帮我?
对您问题的所有答案都是根据此处有关在ggplot中使用Legends的相关问题的答案对代码进行的修改。最简单的方法是添加一个调用,scale_fill_manual()
类似于您已经进行的操作scale_color_manual()
:
ggplot(df) + theme_bw() +
geom_col(aes(Date, value, fill = Legend), position = position_dodge()) +
geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) +
scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") +
scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
theme(legend.position = "bottom") +
scale_fill_manual("", values=c('Daily_Cases'='blue', 'Tests'='red'), labels=c('Numer of Cases', 'Number of Tests'))
在下面给您(您可以将颜色更改为您想要的任何颜色,但是我认为这是您想要的):
正如其他用户指出的那样,您可以使用scale_fill_discrete()
保留默认颜色并仅指定标签:
ggplot(df) + theme_bw() +
geom_col(aes(Date, value, fill = Legend), position = position_dodge()) +
geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) +
scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") +
scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
theme(legend.position = "bottom") +
scale_fill_discrete("", labels=c('Numer of Cases', 'Number of Tests'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句