Secondary axis in R

I want to create a plot with secondary axis, I am able to code using GGPLOT as follows

g1<- ggplot(data = tmp6,aes(x=Month, y=Total,group = Teams)) +
  geom_line(aes(color = Teams),size = 1)+
  geom_bar(aes(x=Month,y=Failed,color=Teams,group=Teams))+
  geom_point(aes(color = Teams),size = 3)+
  ggtitle(label = paste('Total for all Teams'))+

  scale_y_continuous(sec.axis = sec_axis(name="secondary axis"))+
  
  labs(x = "Month", y = "Total") +
   theme(axis.text.x = element_text(angle=45, hjust = 1))
g1

I want to get total in left y axis with line and failed in right y axis with bar, my x axis will be month
I am not getting the secondary plot here, any idea.

Notice that I scaled the y value of geom_col up by the same factor that I reduced the value in sec_axis().

library(ggplot2)
tmp6 <- data.frame(Month = c(1,1,2,2,3,3,4,4),
                 Teams = c("A", "B", "A", "B", "A", "B", "A", "B"),
                 Total = c(1000, 1200, 1150, 1220, 1300, 1030, 1060, 1380),
                 Failed = c(6,4,7,8,2,6,9,4))
ggplot(data = tmp6,aes(x=Month, y=Total,group = Teams)) +
  geom_line(aes(color = Teams),size = 1)+
  geom_col(aes(x=Month,y=Failed * 100, fill=Teams), position = "dodge") +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ . /100, name="Failed (bars)"))+
  labs(x = "Month", y = "Total (lines)")

Created on 2020-08-06 by the reprex package (v0.3.0)

1 Like

Thank you FJCC, I tried this with my data and got bar chat but the line disappears. I hope its issue with secondary axis scale. Please can you suggest how to make scale for below kind of data,

Total      Failed
812212     1381
441485     4212
63576      836 
81241      1808

I made an example of using two axes with the data you provided but I do not like the way it looks. The large variation in both the Total values and the ration between Total and Failed make the graph hard to read. In the second graph, I used a logarithmic scale for y and I think that looks better.

library(ggplot2)
DF <- data.frame(X = 1:4, Total = c(812212, 441485, 63576, 81241),
                 Failed = c(1381, 4212, 836, 1808))

ggplot(DF, aes(X))  + 
  geom_col(aes(y = Failed*250), fill = "skyblue") +
  geom_line(aes(y = Total)) + geom_point(aes(y = Total)) +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ . /250, name="Failed (bars)")) +
  labs(y = "Total")


ggplot(DF, aes(X)) + geom_line(aes(y = Total)) + 
  geom_col(aes(y = Failed), fill = "skyblue") + scale_y_log10() +
  labs(y = "Total (line) or Failed (bars)")

Created on 2020-08-07 by the reprex package (v0.3.0)

1 Like

It works, thank you. But I need to get the Total in(secondary axis) bar instead of line and Failed in primary with line.

That would be

DF <- data.frame(X = 1:4, Total = c(812212, 441485, 63576, 81241),
                 Failed = c(1381, 4212, 836, 1808))

ggplot(DF, aes(X))  + 
  geom_col(aes(y = Total/250), fill = "skyblue") +
  geom_line(aes(y = Failed)) + geom_point(aes(y = Failed)) +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 250, name="Total (bars)")) +
  labs(y = "Failed (line)")
1 Like

Thank you so much FJCC. I am able to achieve the secondary axis as per need. Thanks for your help I really appreciate it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.