Set Y limits for ggplot with sec.axis
0
0
Entering edit mode
14 months ago
JirMan ▴ 20

Hi everyone! I want to display two datasets in one graph with two Y-axes using ggplot and I basically have two main questions. 1. I managed to do so with the sec.axis function, but now I would like to set the limits of my primary Y-axis, e.g. from 85% to 95%, to make the changes in the respective dataset more visible. 2. You may have noticed that I have standard deviation values in the dataframe as well, is there a way to add them with geom.line? My plot looks like this:

enter image description here

This is the script I came up with:

Percent_reads <-data.frame(Mean1= c(93.63,92.19,91.65,87.12), sd1= c(0.3011644,0.9479627,0.7534587,0.7054313), Mean2= c(0.000868,0.025712, 0.900571, 6.034262), sd2= c(0.000218956,0.004187169,0.052000126,0.598933192), Category= c("0hpi","3hpi", "6hpi","9hpi"))

scaleFactor <- max(Percent_reads$Mean1) / max(Percent_reads$Mean2)

Reads_plot <- ggplot(Percent_reads, aes(x=Category)) + labs(x="Hours post infection")+ labs(title="Reads percentage (%)")+ geom_line(aes(y=Mean1), col="darkgreen", group=1)+ geom_line(aes(y=Mean2 * scaleFactor), col="purple", group=1) + scale_y_continuous(name="Host", sec.axis=sec_axis(~./scaleFactor, name="Virus")) + theme( axis.title.y.left=element_text(color="darkgreen"), axis.text.y.left=element_text(color="darkgreen"), axis.title.y.right=element_text(color="purple"), axis.text.y.right=element_text(color="purple") )

Reads_plot

I would be grateful for any tips or suggestions on how to solve this issue :) Thank you in advance!

rstudio ggplot • 499 views
ADD COMMENT

Login before adding your answer.

Traffic: 1957 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6