简体   繁体   中英

Scaling of second data set in ggplot2 line plot

I want to display the second data set (orange line, "Annual") on the secondary axis because its values are much smaller than the values of the first data set (red line, "Cumulative"). I managed to re-scale the seconary y-axis, but I have problems with re-scaling the second data set because both data sets are read in together and I don't know how to treat the second data set separatly in the code.

The data look like this

Year Cumulative Annual 1960 1 1 1961 1 0 1962 1 0 1963 2 1 ... 2019 334 9

I would appreciate any constructive comments!

library("ggplot2")
library("reshape2")
library("tidyverse")

sec_scale=1/10
datu_sub=data[,c(2,3,1)]
datu=melt(datu_sub,id=c("Year"))
p1<-ggplot(datu)+geom_line(aes(x=Year,y=value,colour=variable),linetype="solid",size=1.1)+
  geom_point(aes(x=Year,y=value,colour=variable),shape=1,size=3,stroke=1.5)+
  scale_colour_manual(values=c("darkorange","red"))+
  scale_y_continuous(sec.axis=sec_axis(~.*sec_scale,name="Annual\n"))
p1<-p1+labs(x="\nYear",y="Cumulative\n")
p1+theme(axis.title.x=element_text(size=18),
         axis.text.x=element_text(size=14),
         axis.title.y=element_text(size=18),
         axis.text.y=element_text(size=14),
         axis.ticks=element_blank(),
         legend.title=element_blank(),
         legend.position=c(0.12,0.89),
         legend.text=element_text(size=14),
         legend.background=element_rect(fill="white",size=0.5,linetype="dotted"))

I couldn't fit everything in a comment, so I'll write it as an answer. I had to improvise some data because I don't have a usable sample of yours, but the plotting code should be similar.

sec_scale=1/10
# I made up some data because I don't have yours
datu <- data.frame(Year = c(2000:2010, 2000:2010),
                   value = c(rnorm(11, 500, 10), rnorm(11, 5000, 100)),
                   variable = rep(c("Annual", "Cumulative"), each = 11))

Now for the plotting, I've removed the mapping / aes() arguments from the geoms to the main ggplot function and the geoms will inherit these mappings from the main ggplot call.

Next, you'll notice that I've redefined y = ifelse(variable == "Cumulative, sec_scale, 1) * value , which will scale all value s for which the variable == "Cumulative" by the amount in sec_scale

p1<-ggplot(datu, aes(x = Year, 
                     y = ifelse(variable == "Annual", 1/sec_scale, 1) * value, 
                     colour = variable)) + 
  geom_line(linetype = "solid", size = 1.1) +
  geom_point(shape = 1, size = 3, stroke = 1.5) +
  scale_colour_manual(values = c("darkorange", "red")) +
  scale_y_continuous(sec.axis = sec_axis(~.*sec_scale,name = "Annual\n")) +
  labs(x="\nYear",y="Cumulative\n") + 
  theme(axis.title.x = element_text(size=18),
        axis.text.x = element_text(size=14),
        axis.title.y = element_text(size=18),
        axis.text.y = element_text(size=14),
        axis.ticks = element_blank(),
        legend.title = element_blank(),
        legend.position = c(0.12,0.89),
        legend.text = element_text(size=14),
        legend.background = element_rect(fill="white",size=0.5,linetype="dotted"))

For me the plot looked like this:

在此处输入图片说明

However, I would like to note that a some people dislike secondary axes because they can misleading.

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