英文:
How to fix second y-axis on combined bar and line plot
问题
I am trying to produce a combined bar and line plot. I've developed some code, but it seems like there is some problem with perhaps the range? The bars show up fine, but the line is too low on the graph and the second y-axis tick marks are missing. Does anyone know how I fix this?
#Entering Data
year <- c("2016", "2017", "2018", "2019", "2020", "2021", "2022")
nike <- c(2.8, 3.1, 2.9, 3.1, 3.6, 4.7, 5.1)
jordan <- c(137.7, 154.9, 142.8, 156.9, 180.5, 235.6, 255.0)
data <- data.frame(year = year, nike = nike, jordan = jordan)
adjust <- 2000 # for adjusting the second y-axis
# Plotting Charts and adding a secondary axis
library(ggplot2)
colors <- c("Jordan Brand Revenue (in $B)" = "black",
"Michael Jordan Earnings (in $M)" = "red3") # For adding a legend.
ggplot(data, aes(x=year))+
geom_col(aes(y = nike, color="Jordan Brand Revenue"), lwd = 1 , fill = "black") +
geom_line(aes(y = jordan/adjust, color="Michael Jordan Earnings"), lwd = 1.5, group = 1)+
scale_y_continuous(
name = "Jordan Brand Revenue (in $B)",
breaks = seq(0,6,1),
limits = c(0,6),
sec.axis = sec_axis(~.*adjust, name = "Michael Jordan Earnings (in $M)",
labels = scales::dollar,
breaks= seq(0,300,50))) +
theme_classic() +
scale_color_manual(values=colors)+
theme(axis.title.x=element_blank(),
panel.grid.major.y = element_line(),
legend.title = element_blank(),
legend.position = "top")
英文:
I am trying to produce a combined bar and line plot. I've developed some code, but it seems like there is some problem with perhaps the range? The bars show up fine, but the line is too low on the graph and the second y-axis tick marks are missing. Does anyone know how I fix this?
#Entering Data
year <- c("2016", "2017", "2018", "2019", "2020", "2021", "2022")
nike <- c(2.8, 3.1, 2.9, 3.1, 3.6, 4.7, 5.1)
jordan <- c(137.7, 154.9, 142.8, 156.9, 180.5, 235.6, 255.0)
data <- data.frame(year = year, nike = nike, jordan = jordan)
adjust <- 2000 # for adjusting second y-axis
# Plotting Charts and adding a secondary axis
library(ggplot2)
colors <- c("Jordan Brand Revenue (in $B)" = "black",
"Michael Jordan Earnings (in $M)" = "red3") # For adding a legend.
ggplot(data, aes(x=year))+
geom_col(aes(y = nike, color="Jordan Brand Revenue"), lwd = 1 , fill = "black") +
geom_line(aes(y = jordan/adjust, color="Michael Jordan Earnings"), lwd = 1.5, group
= 1)+
scale_y_continuous(
name = "Jordan Brand Revenue (in $B)",
breaks = seq(0,6,1),
limits = c(0,6),
sec.axis = sec_axis(~.*adjust, name = "Michael Jordan Earnings (in $M)",
labels = scales::dollar,
breaks= seq(0,300,50))) +
theme_classic() +
scale_color_manual(values=colors)+
theme(axis.title.x=element_blank(),
panel.grid.major.y = element_line(),
legend.title = element_blank(),
legend.position = "top")
答案1
得分: 1
Your adjust
value is wrong. I changed it to 50.
Your initial value would've worked better if your data was actually in billions and millions, but the raw data really looks like the difference between a single-digit value vs. a value in the hundreds. An easy way to figure out the best adjust is to do some basic algebra, with the number for the original y-axis on the left and the number you want to correlate on the secondary axis on the right.
In this case, that looks like:
orig.x * adjust = orig.y
6 * adjust = 300
adjust = 300/6
adjust = 50
Here's the code:
adjust <- 50 # for adjusting second y-axis
ggplot(data, aes(x=year)) +
geom_col(aes(y = nike, color="Jordan Brand Revenue (in $B)"), lwd = 1 , fill = "black") +
geom_line(aes(y = jordan/adjust, color="Michael Jordan Earnings (in $M)"), lwd = 1.5, group = 1)+
scale_y_continuous(
name = "Jordan Brand Revenue (in $B)",
breaks = seq(0,6,1),
limits = c(0,6),
sec.axis = sec_axis(~.*adjust, name = "Michael Jordan Earnings (in $M)", labels = scales::dollar, breaks= seq(0,300,50))) +
theme_classic() +
scale_color_manual(values=colors)+
theme(axis.title.x=element_blank(),
panel.grid.major.y = element_line(),
legend.title = element_blank(),
legend.position = "top")
英文:
Your adjust
value is wrong. I changed it to 50.
Your initial value would've worked better if your data was actually in billions and millions, but the raw data really looks like the difference between a single digit value vs. a value in the hundreds. An easy way to figure out the best adjust is to do some basic algebra, with the number for the original y axis on the left and the number you want to correlate on the secondary axis on the right.
In this case, that looks like:
orig.x * adjust = orig.y
6 * adjust = 300
adjust = 300/6
adjust = 50
Here's the code:
adjust <- 50 # for adjusting second y-axis
ggplot(data, aes(x=year))+
geom_col(aes(y = nike, color="Jordan Brand Revenue (in $B)"), lwd = 1 , fill = "black") +
geom_line(aes(y = jordan/adjust, color="Michael Jordan Earnings (in $M)"), lwd = 1.5, group = 1)+
scale_y_continuous(
name = "Jordan Brand Revenue (in $B)",
breaks = seq(0,6,1),
limits = c(0,6),
sec.axis = sec_axis(~.*adjust, name = "Michael Jordan Earnings (in $M)", labels = scales::dollar, breaks= seq(0,300,50))) +
theme_classic() +
scale_color_manual(values=colors)+
theme(axis.title.x=element_blank(),
panel.grid.major.y = element_line(),
legend.title = element_blank(),
legend.position = "top")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论