【问题标题】:Multiple line and bar chart in ggplot with geom_text and coloursggplot中带有geom_text和颜色的多条折线图和条形图
【发布时间】:2015-10-22 11:43:00
【问题描述】:

我想使用 ggplot 将折线图和条形图与三个不同的变量结合起来。条形图的顶部应该有值,并且应该用与上图中的线条相同的颜色填充。这是我的代码

# First some data generation
df.x = data.frame(date = Sys.Date()-20:1,
                  value = c(1:20),
                  variable = "line",
                  object = "x")

df.y = data.frame(date = Sys.Date()-20:1,
                  value = c(1:20)*2,
                  variable = "line",
                  object = "y")

df.z = data.frame(date = Sys.Date()-20:1,
                  value = c(1:20)*3,
                  variable = "line",
                  object = "z")

df.y.bar = data.frame(date = Sys.Date()-10,
                  value = 30,
                  variable = "bar",
                  object = "y")

df.x.bar = data.frame(date = Sys.Date()-10,
                      value = 40,
                      variable = "bar",
                      object = "x")

df.z.bar = data.frame(date = Sys.Date()-10,
                      value = 50,
                      variable = "bar",
                      object = "z")

df = rbind(df.x, df.y,df.z, df.x.bar, df.y.bar,df.z.bar)

my.cols = c("blue", "green", "yellow")

# Pass everything to ggplot
ggplot(df, aes_string(x = "date", y = "value", fill="variable")) +  
  facet_grid(variable~., scales="free_y") + 
  geom_line(data = subset(df, variable == "line"), aes(colour = factor(object)), size = 1, show_guide = FALSE, stat="identity") +
  geom_bar(data = subset(df, variable == "bar"), aes(colour = factor(object)), show_guide = TRUE, stat="identity", position = "dodge") +
  geom_text(data = subset(df, variable == "bar"), aes(y=value+0.7 * sign(value), ymax=value, label=round(value, 2)), position = position_dodge(width = 0.9), size=3) + 
  scale_colour_manual(values = my.cols)

由此产生的情节并不是我想要的。我使用了position_dodge(width = 0.9),但横条上的文字没有移动。令人遗憾的是,当我只选择两个变量(例如 x 和 y)包含在数据框中时,这不会发生。期望的结果应该是这样的:

非常感谢您的帮助!

【问题讨论】:

  • 我的回答有帮助吗?
  • 嗨甜菜根。它适用于您的数据。我在我的数据集上进行了尝试,条形图的颜色效果很好。不幸的是,即使我应用 group Argument,栏上方的文本仍然没有移动...
  • 奇怪,能不能发一些数据和代码来重现文字问题?
  • 它可能远远超出上述示例的简单性。我还是应该发布代码吗?

标签: r charts ggplot2


【解决方案1】:

您可以通过选择fill = factor(object) 并添加scale_fill_manual(values = my.cols) 来填充这些条形。 为了只有一个图例,我认为从aes_string 中删除fill="variable"scale_fill_manual 结合使用可以达到目的。 为了躲避文本,您需要添加 group 参数和 position = position_dodge(width=1)

ggplot(df, aes_string(x = "date", y = "value")) +  
          facet_grid(variable~., scales="free_y") + 
          geom_line(data = subset(df, variable == "line"), aes(colour = factor(object)), size = 1, show_guide = FALSE, stat="identity") +
          geom_bar(data = subset(df, variable == "bar"), aes(fill = factor(object)), show_guide = TRUE, stat="identity", position = "dodge") +
          geom_text(data =subset(df, variable == "bar"), aes(x=date, y=(value+0.7) * sign(value), ymax=value, label=round(value, 2), group=object), position = position_dodge(width=1), size=3) +
          scale_colour_manual(values = my.cols) +
          scale_fill_manual(values = my.cols)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2021-12-23
    • 2017-12-24
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多