r - 如何将点添加到堆叠条形图

标签 r ggplot2 geom-bar ggplotly

我想在我的已经添加点(变量 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") 

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

最佳答案

您的列 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") 

enter image description here

关于r - 如何将点添加到堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50447236/

相关文章:

R ggplot2 : how do I plot an implicit function (contour line at just one level)?

r - ggplot2 中每个方面的不同轴限制

r - 带有计数的堆叠条形图中 geom_text 的百分比

r - ggplot2 和带有负值的堆积条形图

r - 计算每个字符并存储为 <K,V> 对

r - 绘制带有正方形和线条的图的放大插图图

R 滞后于缺失数据

r - 为什么数据帧迭代中的逻辑运算不返回正确的值?

r - 使用ggplot,如何自动设置时间序列图的x轴?

r - geom_bar() : stacked and dodged combined