我有以下数据帧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))

我想用不同的 colored颜色 和标签在同一张图中画出变量FalseTrue.我想用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()

不起作用(图形为空).

推荐答案

我们必须使用group=1来绘制线条:

ggplot(prop_data, aes(x = Month, group=1)) +
  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()

enter image description here

R相关问答推荐

在Julia中调用R函数

如何将y轴上的线定位得彼此更近

多个ggpredicate对象的平均值

使用R中的Shapetime裁剪格栅文件

咕噜中的元素列表:map

有没有一种方法可以从函数中创建一个值的列表,然后将这些值变成R中的直方图?我一直觉得不行

如何按排序顺序打印一个框架中所有精确的唯一值?

如何利用模型函数在格图中添加双曲/指数曲线

筛选出以特定顺序患病的个体

矩阵的堆叠条形图,条形图上有数字作为标签

无法正确设置动态创建的Quarto标注的格式

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

根据现有列的名称和字符串的存在进行变异以创建多个新列

使用范围和单个数字将数字与字符串进行比较

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

`夹心::vcovCL`不等于`AER::tobit`标准错误

为什么函数toTitleCase不能处理english(1),而toupper可以?

我需要使用ggplot2制作堆叠条形图

在使用SliderInput In Shiny(R)设置输入数据的子集时,保留一些情节痕迹

对一个数据帧中另一个数据帧中的值进行计数