[英]How to plot combo plot in R using ggplot
my data frame i am using is我正在使用的数据框是
the output I'm trying to achieve我试图实现的 output
I achieved this output in excel and I'm trying to reproduce it in R but I am not able to get the 2 lines I tried using the code
我在 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))
this is the output I get
这是我得到的 output
here is my reproduceable sample of data frame这是我可复制的数据框样本
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))
As the message suggest you have to set the group
aes and as you probably want colored lines you have to map on the color
aes additionally.正如消息所示,您必须设置
group
aes,并且您可能需要彩色线条,因此您还必须在color
aes 上设置 map。 Also note, that your columns are not numerics but characters.另请注意,您的列不是数字而是字符。 Hence you have to convert them to numerics and instead format as percentages via the
labels
argument of scale_y_continuous
:因此,您必须将它们转换为数字,而是通过
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.