简体   繁体   English

如何将点添加到堆积的条形图中

[英]how to add points to stacked barplots

I would like to add points (the variable nb species) on my already existing chart. 我想在我现有的图表上添加点(变量nb种类)。 I tried to add the points with the function geom_point but unfortunately i have this error: 我试图用函数geom_point添加点但不幸的是我有这个错误:

"Discrete value supplied to continuous scale".

You will find below my data and my code. 您将在下面找到我的数据和代码。

  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") 

Thanks a lot for take time to help me !! 非常感谢您花时间帮助我!

Your column nb_species is in character format, convert it to numeric and you are good to go. 你的列nb_species是字符格式,将其转换为数字,你很高兴。

A quick str(complet_b) would tell you that your column format. 快速str(complet_b)会告诉你你的列格式。

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

Here is your code and plot with the dot present in the graph: 这是您的代码和图表,图中有一个点:

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") 

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM