简体   繁体   中英

How to create a line and bar chart together in R

Hi sorry if this post is formated wrong I've never posted on this form. I was looking for help on creating a bar chart and line graph on one chart. The code I have below is what I have so far. However, when I actually chart the plots I get the graph attached. Where am I going wrong?

在此处输入图像描述


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


Dput Data

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

As I don't know about the data, please notice that it's just a guess.

There's nothing wrong, but just values between Revenue and Users are having very different scale, so it would be better using secondary axis.

For example, I made this df dummy data.

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

Then simply try

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

在此处输入图像描述

Note that 800 in code above is from ratio between largest number between Revenue and Users

Add for your data

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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