Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot with ggplot a graph with two y scales [duplicate]

Tags:

So I am trying to compile several dataframe in one graph. For my x value I have the number of days, and for y I have the frequency of something happening. My problem is that one of the dataframe (df3) ranges from 0 to 3, and the others from 0 to 50.

Dummy dataframes:

day<-c(1,2,3)
b<-c(23,44,22)
c<-c(12,35,49)
d<-c(1,1,3)

df1<-data.frame(day,b)
df2<-data.frame(day,c)
df3<-data.frame(day,d)


ggplot()+ 
  geom_line(data=df1, aes(x=day, y=df1$`b`), color="red") +
  geom_line(data=df2, aes(x=day, y=df2$`c` ), color="green")+
  geom_line(data=df3, aes(x=day, y=df3$`d` ), color="blue")+
  labs(x="Days", y="Number of occurrences")

This works fine, but I want to creat a diferent scale for df1 and df2, and another to df3.

UPDATE:

I am trying this, but it overwrites the previous scale:

d<-ggplot()+ 
  geom_line(data=df1, aes(x=day, y=df1$`a`), color="red") +
  geom_line(data=df2, aes(x=day, y=df2$`b` ), color="green")+
  scale_y_continuous(limits=c(0, 50))+
  labs(x="Days", y="Number of occurrences")

d+geom_line(data=df3, aes(x=day, y=df3$`c` ), color="blue")+
  scale_y_continuous(limits=c(0, 3))
like image 797
L8call Avatar asked Jul 02 '18 08:07

L8call


People also ask

Can we plot two Y axis on the same chart in Highcharts?

Chart showing use of multiple y-axes, where each series has a separate axis. Multiple axes allows data in different ranges to be visualized together. While this in some cases can cause charts to be hard to read, it can also be a powerful tool to illustrate correlations.

What is a double y plot?

A second Y axis is a Y axis drawn on the right-hand side of a chart. It can show the same axis scale as the primary Y axis or a different scale. You can use a second Y axis with the same scale as the primary Y axis on a wide chart to help viewers interpret the data more easily.

What is %>% in Ggplot?

the %>% is a pipe operator that is actually part of the dplyr library (along with the filter function) not from the ggplot2 library. To sample 1%, there is a sample_frac function in the dplyr library.


1 Answers

I think you can do it using the sec.axis param of ggplot2:

    d<-ggplot()+ 
      geom_line(data=df1, aes(x=day, y=df1$`1`), color="red") +
      geom_line(data=df2, aes(x=day, y=df2$`1` ), color="green")+
      scale_y_continuous(limits=c(0, 50))+
      labs(x="Days", y="Number of occurrences")
    
    d+geom_line(data=df3, aes(x=day, y=df3$`1` ), color="blue")+
      scale_y_continuous(limits=c(0, 3),
           sec.axis = sec_axis(~ . *scale_of_the_new_axis, name = "name of the new axis")
      )

Note that I added this line on your code:

    sec.axis = sec_axis(~ . *scale_of_the_new_axis, name = "name of the new axis")

EDIT:

I applied a transformation to the data of df3, then I applied the inverse of the transformation to have the real values of df3 reflected on the new axis.

ggplot()+ 
        geom_line(data=df1, aes(x=day, y=b), color="red") +
        geom_line(data=df2, aes(x=day, y=c ), color="green")+
        geom_line(data=df3, aes(x=day, y=d*50/3), color="blue")+
        scale_y_continuous(limits=c(0, 50), 
                           sec.axis = sec_axis(~ . *3/50, name = "name of the new axis"))+
        labs(x="Days", y="Number of occurrences")

The result is this: enter image description here

Let me know if this is what you want.

like image 121
Riverarodrigoa Avatar answered Oct 01 '22 14:10

Riverarodrigoa