【问题标题】:how to add points to stacked barplots如何向堆叠的条形图添加点
【发布时间】:2018-05-21 10:50:54
【问题描述】:

我想在我已经添加点(变量 nb 物种) 现有图表。我尝试使用函数geom_point 添加点,但不幸的是我有这个错误:

"Discrete value supplied to continuous scale".

你会在下面找到我的数据和我的代码。

  structure(list(SOUNAME = c("BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", 
"BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)"
), year_month = c("2014-05", "2014-05", "2014-05", "2014-05", 
"2014-06", "2014-06", "2014-06", "2014-06", "2014-07", "2014-07", 
"2014-07", "2014-07"), pre_type = c("NONE", "HEAVY", "LIGHT", 
"MEDIUM", "NONE", "HEAVY", "LIGHT", "MEDIUM", "NONE", "HEAVY", 
"LIGHT", "MEDIUM"), pre_value = c(3, 6, 20, 2, 16, 2, 9, 2, 3, 
3, 22, 3), tem_type = c("V_COLD", "COLD", "HOT", "MEDIUM", "V_COLD", 
"COLD", "HOT", "MEDIUM", "V_COLD", "COLD", "HOT", "MEDIUM"), 
    tem_value = c(0, 31, 0, 0, 0, 24, 6, 0, 0, 23, 8, 0), nb_species = c("NA", 
    "3", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", 
    "NA"), x = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L)), .Names = c("SOUNAME", "year_month", "pre_type", "pre_value", 
"tem_type", "tem_value", "nb_species", "x"), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(data = complet_b, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") +
          scale_x_continuous(breaks=1:3, labels=unique(complet_b$year_month)) +
          geom_bar(stat = "identity", width=0.3) +
          xlab("date") + ylab ("Number of days of precipitation") +
          ggtitle("Precipitation per month") + labs(fill = "Frequency") +
          geom_bar(data=complet_b,aes(x=x+0.4, y=tem_value, fill=tem_type), width=0.3, stat = "identity") +
          xlab("date") + ylab("Number of days of temperature") +
          ggtitle("Temperature per month") + labs(fill = "Frequency") 

非常感谢您花时间帮助我!

【问题讨论】:

    标签: r ggplot2 geom-bar ggplotly


    【解决方案1】:

    您的列nb_species 是字符格式,将其转换为数字即可。

    快速的str(complet_b) 会告诉您您的列格式。

    complet_b$nb_species <- as.numeric(complet_b$nb_species)
    

    这是您的代码和图表中出现的点:

    library(ggplot2)
    ggplot(data = complet_b, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") +
      scale_x_continuous(breaks=1:3, labels=unique(complet_b$year_month)) +
      geom_bar(stat = "identity", width=0.3) +
      xlab("date") + ylab ("Number of days of precipitation") +
      ggtitle("Precipitation per month") + labs(fill = "Frequency") +
      geom_bar(data=complet_b,aes(x=x+0.4, y=tem_value, fill=tem_type), width=0.3, stat = "identity") +
      xlab("date") + ylab("Number of days of temperature") +
      geom_point(aes(x = x, y= nb_species)) +
      ggtitle("Temperature per month") + labs(fill = "Frequency") 
    

    【讨论】:

    • 非常感谢您的回答。是否可以将两列之间的点居中/移动?横坐标轴也一样?
    • @ElodieP.,如果解决了问题,请接受(绿色复选标记)/upvote the solution,在移动点时,您可以移动点,但这里该点的值是 3 on 2014-05,所以除非您更改值,否则您无法移动它。你可以看到你的数据。 stackoverflow.com/help/someone-answers
    • 对不起,我是新手,我没见过……可以移动胸部,不是向上或向下而是向右移动?
    • @ElodieP.,它取决于你的 x 轴,如果这个值出现在日期为 2014-06 的行上,它会在中心,就像我说的那样,它完全取决于您的输入数据,尝试在一张纸上绘图,您会更了解它。但是,可能还有另一种方法,但不太可能
    • 是的,但我不明白为什么点放在 barplot pre_type 上,而不是放在中间..? (在 barplot tem 和 barplot pre 之间)
    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多