r - 如何向堆叠的条形图添加点
问题描述
我想在我已经存在的图表上添加点(变量 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")
推荐阅读
- javascript - 在 timeLeft = 0 时重置计时器
- java - 如何在 sping-webflux 中制作 Mono/Flux 链并实现 Retry
- python-turtle - 如何创建具有可更改颜色的叠加层的螺旋线
- arrays - 反应原生中具有多级嵌套数组的部分列表
- python - Pyspark - 读取 csv 文件并保留原始特殊字符
- java - 打开跟踪未显示跨度之间的关系
- azure-functions - 在 Azure Function 横向扩展期间似乎卸载程序集的问题
- ruby - Ruby Google Drive API -- 统一常量 OOB_URI
- bash - 在unix bash中按行和列提取信息
- python - 无法弄清楚如何将用户输入的整数计算为阶乘