[英]How to plot combo plot in R using ggplot
我试图实现的 output 我在 excel 中实现了这个 output 并且我试图在 R 中重现它,但我无法获得我尝试使用代码的 2 行
ggplot(yearly_percentage) +
geom_bar(aes(x=year, y=count_percentage, fill=member_casual), position = "dodge",stat = "identity") +
geom_line( aes(x=year,y=ride_Length))
这是我可复制的数据框样本
structure(list(year = c("2016", "2016", "2017", "2017", "2018",
"2018", "2019", "2019", "2020", "2020"), member_casual = c("casual",
"member", "casual", "member", "casual", "member", "casual", "member",
"casual", "member"), count_percentage = c("23.88%", "76.12%",
"21.86%", "78.14%", "18.79%", "81.21%", "23.06%", "76.94%", "11.31%",
"88.69%"), ride_Length = c("45%", "55%", "42%", "58%", "46.6%",
"53.4%", "51.5%", "48.5%", "42%", "58%")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L))
正如消息所示,您必须设置group
aes,并且您可能需要彩色线条,因此您还必须在color
aes 上设置 map。 另请注意,您的列不是数字而是字符。 因此,您必须将它们转换为数字,而是通过scale_y_continuous
的labels
参数格式化为百分比:
library(ggplot2)
yearly_percentage$count_percentage <- readr::parse_number(yearly_percentage$count_percentage)
yearly_percentage$ride_Length <- readr::parse_number(yearly_percentage$ride_Length)
ggplot(yearly_percentage) +
geom_bar(aes(x = year, y = count_percentage, fill = member_casual), position = "dodge", stat = "identity") +
geom_line(aes(x = year, y = ride_Length, color = member_casual, group = member_casual)) +
scale_y_continuous(labels = scales::label_percent(scale = 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.