如何在R中一起创建折线图和条形图[重复]

Posted

技术标签:

【中文标题】如何在R中一起创建折线图和条形图[重复]【英文标题】:How to create a line and bar chart together in R [duplicate] 【发布时间】:2022-01-07 18:23:57 【问题描述】:

您好,如果这篇文章的格式有误,我很抱歉,我从未在此表单上发布过。我正在寻求在一个图表上创建条形图和折线图的帮助。我下面的代码是我到目前为止所拥有的。但是,当我实际绘制图表时,我会附上图表。我哪里错了?


applemusic_subs_rev <- read_csv("apple_music_revenue_and_subs.csv", show_col_types = F)

applemusic_subs_rev2 <-  subset(applemusic_subs_rev, select = c(1:3))  [-c(7),]

applemusic_subs_rev2 <- applemusic_subs_rev2 |> filter(!is.na(Revenue)) |> 
  
  mutate(Revenue = gsub("[\\$,]", "", Revenue),
         Revenue = gsub("billion", "", Revenue),
         Users = gsub("million", "", Users))

applemusic_subs_rev2$Revenue <- as.numeric(as.character(applemusic_subs_rev2$Revenue))
applemusic_subs_rev2$Users <- as.numeric(as.character(applemusic_subs_rev2$Users)) |>

  
mutate(Revenue = Revenue*1000)



    
applemusic_subs_rev2 <- applemusic_subs_rev2|>
mutate(Revenue = Revenue*1000)
#Revenue in Millions 

df <- data.frame(applemusic_subs_rev2)
#Data frame for graph 


ggplot(df) +
  geom_bar(aes(x = Year , weight = Users), fill = "grey70") +
  geom_line(aes(x = Year, y = Revenue, colour = "yellow")) + 
  scale_colour_manual(name = "Legend", labels = c("Value in 100"), values = c("black"))


输入数据

structure(list(Year = c(2015, 2016, 2017, 2018, 2019, 2020, NA
), Revenue = c(NA, "$0.6 billion", "$1.1 billion", "$1.8 billion", 
"$2.8 billion", "$4.1 billion", NA), Users = c("11 million", 
"20 million", "27 million", "40 million", "50 million", "72 million", 
NA), ...4 = c(NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
-7L), spec = structure(list(cols = list(Year = structure(list(), class = c("collector_double", 
"collector")), Revenue = structure(list(), class = c("collector_character", 
"collector")), Users = structure(list(), class = c("collector_character", 
"collector")), ...4 = structure(list(), class = c("collector_logical", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x00000250d76dea50>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

【问题讨论】:

欢迎来到 SO!它有助于提供数据以提供帮助。您可以使用dput(applemusic_subs_rev) 来做到这一点,然后编辑您的问题并粘贴结果。另外,看看:How to create a Minimal, Reproducible Example 【参考方案1】:

由于我不知道数据,请注意这只是一个猜测。

没有错,只是RevenueUsers 之间的值具有非常不同的比例,因此使用辅助轴会更好。

例如,我做了这个df 虚拟数据。

df <- data.frame(
  Year = c(2016,2017,2018,2019,2020),
  Revenue = c(600,1050,1700,2700,4100),
  Users = c(1,2,3,4,5)
)
  Year Revenue Users
1 2016     600     1
2 2017    1050     2
3 2018    1700     3
4 2019    2700     4
5 2020    4100     5

那就试试吧

ggplot(df) +
  geom_bar(aes(x = Year , weight = Users*800), fill = "grey70") +
  geom_line(aes(x = Year, y = Revenue, colour = "yellow")) +
  scale_y_continuous(name = "Revenue", sec.axis = sec_axis( ~ ./800, name = "Users")) +
  scale_colour_manual(name = "Legend", labels = c("Value in 100"), values = c("black")) 

注意上面代码中的800RevenueUsers之间的最大数之比

为您的数据添加

ggplot(df) +
  geom_bar(aes(x = Year , weight = Users*56.9444), fill = "grey70") +
  geom_line(aes(x = Year, y = Revenue, colour = "yellow")) +
  scale_y_continuous(name = "Revenue", sec.axis = sec_axis( ~ ./56.9444, name = "Users")) +
 scale_colour_manual(name = "Legend", labels = c("Value in 100"), values = c("black")) 

【讨论】:

以上是关于如何在R中一起创建折线图和条形图[重复]的主要内容,如果未能解决你的问题,请参考以下文章

R Shiny ggplot 条形图和折线图,具有动态变量选择和 y 轴为百分比

折线图+不同频率的分组条形图[重复]

java生成饼状图,条形图,折线图的技术可以动态的显示

如何创建 QChart.js 折线图

【R语言】--- ggplot2绘制折线图

怎样把柱状图和折线图合并一起