下面是一个dataframe prop_data
:
structure(list(Month = structure(1:10, levels = c("January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"), class = "factor"),
`Count false` = c(3211L, 2293L, 2051L, 1689L, 1203L, 580L,
638L, 394L, 227L, 108L), False = c(0.946918313181952, 0.965880370682393,
0.948220064724919, 0.964041095890411, 0.961630695443645,
0.952380952380952, 0.987616099071207, 0.985, 0.934156378600823,
0.931034482758621), `Count true` = c(180L, 81L, 112L, 63L,
48L, 29L, 8L, 6L, 16L, 8L), True = c(0.0530816868180478,
0.0341196293176074, 0.0517799352750809, 0.035958904109589,
0.0383693045563549, 0.0476190476190476, 0.0123839009287926,
0.015, 0.065843621399177, 0.0689655172413793)), class = "data.frame", row.names = c(NA,
-10L))
我想用不同的颜色和标签在同一个图中绘制变量False
和True
。我想用ggplot2
。下面的代码
library(ggplot2)
ggplot(prop_data, aes(x = Month)) +
geom_line(aes(y = `False`, color = "False"), size = 1.2) +
geom_line(aes(y = `True`, color = "True"), size = 1.2) +
labs(title = "Trend", x = "Month", y = "Proportions") +
scale_color_manual(values = c("False" = "blue", "True" = "red")) +
theme_minimal()
不起作用(图形为空)。
2条答案
按热度按时间d7v8vwbk1#
除了TarJae的答案之外,我还建议在绘制数据之前使用
pivot_longer
:这就避免了对
geom_line()
的重复调用,意味着您不必手动输入颜色值-它们是根据数据中的值提取的。bhmjp9jg2#
我们必须使用
group=1
来绘制线条: