Lalantra
Lalantra

Reputation: 87

Plotting secondary axis using ggplot

i am trying to plot three variable (SA,SA1,SA2) with two variable(SA& SA2) on left y-axis and one variable (SA1)on right secondary y-axis. I tried to fix the axis limits using limits = c(1e15,5e15) on left y-axis while trying to limit secondary axis between limits = c(3e17,4.2e17) but i am unable to plot the seocondary axis with my customized limits. DATA Link

library(ggplot2)
test <- read.xlsx2("filepath/test.xlsx", 1, header=TRUE)
View(test)
test$SA=as.numeric(levels(test$SA))[test$SA]
test$SA1=as.numeric(levels(test$SA1))[test$SA1]
test$SA2=as.numeric(levels(test$SA2))[test$SA2]
g <- ggplot(test,aes(x=year, y=  SA, group = 1)) + geom_line(mapping = aes(x = test$year, y = test$SA)) 
+ geom_line(mapping = aes(x = test$year, y = test$SA2), color = "red") +  geom_line(mapping = aes(x = test$year, y = test$SA1), size = 1, color = "blue")
 g+scale_y_continuous(name = "primary axis title",
+                      sec.axis = sec_axis(~./5, name = "secondary axis title (SA1)"))

enter image description here enter image description here

Final Solution by @dc37 gives me the followibng result:

ggplot(subset(DF, Var != "SA1"), aes(x = year, y = val, color = Var))+
  geom_line()+
  scale_y_continuous(name = "Primary axis", sec.axis = sec_axis(~.*100, name = "Secondary"))

Thanks enter image description here

Upvotes: 2

Views: 10926

Answers (1)

dc37
dc37

Reputation: 16178

The argument sec.axis is only creating a new axis but it does not change your data and can't be used for plotting data.

To do be able to plot data from two groups with a large range, you need to scale down SA1 first.

Here, I scaled it down by dividing it by 100 (because the ratio between the max of SA1 and the max of SA and SA2 is close to 100) and I also reshape your dataframe in longer format more suitable for ggplot2:

library(lubridate)
df$year = parse_date_time(df$year, orders = "%Y") # To set year in a date format
library(dplyr)
library(tidyr)
DF <- df %>% mutate(SA1_100 = SA1/100) %>% pivot_longer(.,-year, names_to = "Var",values_to = "val")

# A tibble: 44 x 3
    year Var         val
   <int> <chr>     <dbl>
 1  2008 SA      1.41e15
 2  2008 SA1     3.63e17
 3  2008 SA2     4.07e15
 4  2008 SA1_100 3.63e15
 5  2009 SA      1.53e15
 6  2009 SA1     3.77e17
 7  2009 SA2     4.05e15
 8  2009 SA1_100 3.77e15
 9  2010 SA      1.52e15
10  2010 SA1     3.56e17
# … with 34 more rows

Then, you can plot it by using (I subset the dataframe to remove "SA1" and keep the transformed column "SA1_100"):

library(ggplot2)
ggplot(subset(DF, Var != "SA1"), aes(x = year, y = val, color = Var))+
  geom_line()+
  scale_y_continuous(name = "Primary axis", sec.axis = sec_axis(~.*100, name = "Secondary"))

enter image description here

BTW, in ggplot2, you don't need to design column using $, simply write the name of it.

Data

structure(list(year = 2008:2018, SA = c(1.40916e+15, 1.5336e+15, 
1.52473e+15, 1.58394e+15, 1.59702e+15, 1.54936e+15, 1.6077e+15, 
1.59211e+15, 1.73533e+15, 1.7616e+15, 1.67771e+15), SA1 = c(3.63e+17, 
3.77e+17, 3.56e+17, 3.68e+17, 3.68e+17, 3.6e+17, 3.6e+17, 3.68e+17, 
3.55e+17, 3.58e+17, 3.43e+17), SA2 = c(4.07e+15, 4.05e+15, 3.94e+15, 
3.95e+15, 3.59e+15, 3.53e+15, 3.43e+15, 3.2e+15, 3.95e+15, 3.03e+15, 
3.16e+15)), row.names = c(NA, -11L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x56412c341350>)

Upvotes: 4

Related Questions