【问题标题】:How to add geom_line to stacked barplot in r如何将geom_line添加到r中的堆叠条形图
【发布时间】:2018-11-21 11:37:33
【问题描述】:

下面是我的代码。我试图在堆叠的条形图顶部添加一行(来自不同 csv 文件的数据),但是它不起作用,错误显示“找不到对象变量”。没有添加 geom_line 堆叠的条形图可以工作,所以我认为它是造成问题的线。关于如何解决此问题的任何想法?

a <- read.csv("data.csv", header=TRUE, sep=",")

line1 <- read.csv("data1.csv", header=TRUE, sep=",")

line2 <- data.frame(line1)

library(reshape2)
c <- melt(a, id.var="day")

library(ggplot2)
a <- ggplot(c, aes(x=day, y=value, fill=variable)) +

 geom_bar(stat="identity", aes(x=day, y=value), width=0.7) +

 geom_line(data=line2, aes(x=day, y=value), color="black", stat="identity") 
 +

 scale_fill_manual(values = c("black", "grey47", "grey")) +

 scale_x_continuous(breaks = round(seq(min(m$day), max(m$day), by = 1),0))

 print(a)

【问题讨论】:

  • 1) read.csv 返回一个data.frame,你不需要line2 &lt;- data.frame(line1)。 2)您可以发布示例数据吗?请使用dput(line1) 的输出编辑问题。或者,如果 dput(head(line1, 20)) 的输出太大。
  • 这是我的 data.csv 示例数据(分组条形图)day,emigration,security,checkin 1,6,5,4 2,6,5,6 3,6,5,9 4 ,6,5,1 5,5,6,3 6,3,6,5 7,1,8,7 8,9,9,9 9,8,9,8 10,7,9,6 11, 6,8,4 12,4,8,2 13,3,5,1 14,1,7,3 15,2,7,4 16,4,6,5 17,5,5,6 18,6 ,5,7 19,8,4,8 20,7,3,8 21,5,2,2 22,4,2,1 23,1,2,2 24,2,2,3 25,4, 4,4 26,9,9,5 27,8,7,7 28,7,6,8 29,6,4,9 30,4,3,1 31,3,2,1
  • 这是data1.csv(线图)日的样本数据,值1,12 2,11 3,10 4,8 5,7 6,6 7,6 8,6 9, 7 10,8 11,14 12,6 13,6 14,6 15,8 16,8 17,10 18,10 19,12 20,12 21,12 22,13 23,13 24,14 25,15 26 ,15 27,10 28,10 29,10 30,10 31,12
  • 我的猜测是你在line2 中没有列名variable,如果你将fill = variable 移动到geom_bar 中的aes 可能会解决问题
  • @LydiaBlakley:请将数据添加到您的问题中以获得更好的格式

标签: r ggplot2 bar-chart


【解决方案1】:

以下是生成下图的完整代码示例。
我已经更改了您的变量名称,以使它们更加一致。您已将文件 "data.csv" 中的 data.frame 和 ggplot 指令的结果命名为 a

library(reshape2)
library(ggplot2)

a <- read.csv("~/data.csv")
line1 <- read.csv("~/data2.csv")

long <- melt(a, id.var = "day")

g <- ggplot(long, aes(x = day, y = value)) +
  geom_bar(aes(x = day, y = value, fill = variable), 
           stat = "identity", width = 0.7) +
  geom_line(data = line1, 
            aes(x = day, y = value), 
            color = "black") +
  scale_fill_manual(values = c("black", "grey47", "grey")) +
  scale_x_continuous(breaks = min(long$day):max(long$day))

print(g)

dput 格式的数据。

a <-
structure(list(day = 1:31, emigration = c(6L, 6L, 6L, 6L, 5L, 
3L, 1L, 9L, 8L, 7L, 6L, 4L, 3L, 1L, 2L, 4L, 5L, 6L, 8L, 7L, 5L, 
4L, 1L, 2L, 4L, 9L, 8L, 7L, 6L, 4L, 3L), security = c(5L, 5L, 
5L, 5L, 6L, 6L, 8L, 9L, 9L, 9L, 8L, 8L, 5L, 7L, 7L, 6L, 5L, 5L, 
4L, 3L, 2L, 2L, 2L, 2L, 4L, 9L, 7L, 6L, 4L, 3L, 2L), checkin = c(4, 
6, 9, 1, 3, 5, 7, 9, 8, 6, 4, 2, 1, 3, 4, 5, 6, 7, 8, 8, 2, 1, 
2, 3, 4, 5, 7, 8, 9, 1, 1)), class = "data.frame", 
row.names = c(NA, -31L))

line1 <-
structure(list(day = 1:31, value = c(12, 11, 10, 8, 7, 6, 6, 
6, 7, 8, 14, 6, 6, 6, 8, 8, 10, 10, 12, 12, 12, 13, 13, 14, 15, 
15, 10, 10, 10, 10, 12)), class = "data.frame", 
row.names = c(NA, -31L))

【讨论】:

    【解决方案2】:

    根据您的数据结构的 cmets,我想它可能有助于先加入您的数据框,然后使用一个数据集构建绘图。你可以试试:

    library(dplyr)
    c <- c %>%
      left_join(line2 %>%
                  rename(value_line2 = value),
                by="day")
    

    然后调整geom_line()

    geom_line(data=c, aes(x=day, y=value_line2), color="black", stat="identity")
    

    这可能会有所帮助。如果加入数据没有按预期工作,请告诉我。

    【讨论】:

      【解决方案3】:

      如果不清楚,这就是我在上面评论中的意思:

      library(ggplot2)
      a <- ggplot(c, aes(x=day, y=value)) +
           geom_bar(stat="identity", aes(x=day, y=value, fill=variable), width=0.7) +
           geom_line(data=line2, aes(x=day, y=value), color="black", stat="identity")
      

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多