如何在组合柱状图和折线图中修复第二个y轴

huangapple go评论68阅读模式
英文:

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 &lt;- c(&quot;2016&quot;, &quot;2017&quot;, &quot;2018&quot;, &quot;2019&quot;, &quot;2020&quot;, &quot;2021&quot;, &quot;2022&quot;)
nike &lt;- c(2.8, 3.1, 2.9, 3.1,  3.6, 4.7,  5.1)
jordan &lt;- c(137.7, 154.9, 142.8, 156.9, 180.5, 235.6, 255.0)

data &lt;- data.frame(year = year, nike = nike, jordan = jordan)
adjust &lt;- 2000 # for adjusting second y-axis

# Plotting Charts and adding a secondary axis
library(ggplot2)
colors &lt;- c(&quot;Jordan Brand Revenue (in $B)&quot; = &quot;black&quot;,
        &quot;Michael Jordan Earnings (in $M)&quot; = &quot;red3&quot;) # For adding a legend.
ggplot(data, aes(x=year))+  
geom_col(aes(y = nike, color=&quot;Jordan Brand Revenue&quot;), lwd = 1 , fill = &quot;black&quot;) +
geom_line(aes(y = jordan/adjust, color=&quot;Michael Jordan Earnings&quot;), lwd = 1.5, group                
= 1)+
scale_y_continuous(
name = &quot;Jordan Brand Revenue (in $B)&quot;,
breaks = seq(0,6,1),
limits = c(0,6),
sec.axis = sec_axis(~.*adjust, name = &quot;Michael Jordan Earnings (in $M)&quot;,
                    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 = &quot;top&quot;)

答案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 &lt;- 50 # for adjusting second y-axis

ggplot(data, aes(x=year))+  
geom_col(aes(y = nike, color=&quot;Jordan Brand Revenue (in $B)&quot;), lwd = 1 , fill = &quot;black&quot;) +
geom_line(aes(y = jordan/adjust, color=&quot;Michael Jordan Earnings (in $M)&quot;), lwd = 1.5, group = 1)+
scale_y_continuous(
name = &quot;Jordan Brand Revenue (in $B)&quot;,
breaks = seq(0,6,1),
limits = c(0,6),
sec.axis = sec_axis(~.*adjust, name = &quot;Michael Jordan Earnings (in $M)&quot;, 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 = &quot;top&quot;)

huangapple
  • 本文由 发表于 2023年5月18日 04:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定