r - ggplot2 second y-Axis, adjusting height -


i made plot ggplot2 second-y-axis example: http://steffi.ca/thinkr/?p=91

everything works fine. problem second y-axis way tall values not match exactly. see picture

plot tall second y axis

the maximum of y-data second y-axis

enter image description here

in picture maximum @ ~19.7

of course adjust 1 dataset(with scale_y_continious) new datasets manage everyday have different values etc...

i made reproducible code:

library(reshape)                    # package melting data library(ggplot2)                    # package beautiful plots library(extrafont)              # package nice fonts library(grid)                   # package smooth grid library(rcolorbrewer)               # package beautiful color palettes library('pracma')                   # linspace function  ### function more , pretty axis-breaks ###  number_ticks <- function(n) {function(limits) pretty(limits, n)}   ### prepare data ###  real_xdata <- seq(from=1,to=60,by=2) real_ydata <- runif(30,0,40)  ### limits of data ###  mmval <- matrix(1,                 nrow = 2,                 ncol = length(real_ydata))                  mmval <- range(as.numeric(real_ydata))                 xylim <- range(as.numeric(mmval))  ### prepare data second y axis ###  real_ydata_two <- runif(30,0,378)  ### create dataframe ggplot2 ###  ggplot_data <- data.frame(matrix(na,nrow=length(real_xdata),ncol=2)) ggplot_data$x1 <- real_xdata ggplot_data$x2 <- real_ydata  ### transform data second y-axis fits in first plot ###  dataset_second_yaxis <- real_ydata_two  sum1 <- summary(dataset_second_yaxis)  dataset_second_yaxis_two <-dataset_second_yaxis*((max(xylim)-min(xylim))/sum1[[6]])+ (min(xylim)-min(dataset_second_yaxis, na.rm=true))  ### add transformed data dataframe ###  i=ncol(ggplot_data)  ggplot_data[i+1]<-dataset_second_yaxis_two  ### color ###  colp <- c( brewer.pal(ncol(ggplot_data),"spectral")[-2])   ggplot_one <- melt(ggplot_data, id = "x1") ### melt data ### ggplot_one <- ggplot(data = ggplot_one, aes(x = x1, y = value, color =variable)) +   geom_point() +     theme_gray() +   scale_color_manual(                       #legend                      labels           = c("ydata_one","ydata_two"),                      values           = colp) +   scale_y_continuous(expand = c(0,0), limits = c(round(xylim[1]-0.04*xylim[2],digits=2), round(xylim[2]+0.04*xylim[2], digits=2)), breaks = number_ticks(10)) +     labs(x = "xaxis", y = "yaxis") +   theme_gray() +   theme(axis.text.x   = element_text(colour = "grey50", size = 10, vjust = 0.5, family = "book antiqua"),         legend.title      = element_blank(),         legend.text       = element_text(colour="grey50", size = 11, family = "book antiqua"),         legend.position   = "top",         legend.key        = element_rect(colour = 'white', fill = 'white'),         panel.background  = element_rect(fill = "grey95"),         panel.grid.minor  = element_line(colour = "grey98", size = 0.25),          panel.grid.major  = element_line(colour = "grey99", size = 0.25),          axis.text.y       = element_text(colour = "grey50", size = 10, family = "book antiqua"),         axis.line     = element_line(colour = "grey50", size=0.25),         axis.ticks    = element_line(size=0.25),         axis.title.x      = element_text(colour = "black", size = 14, vjust=-0.5, family= "book antiqua"),         axis.title.y      = element_text(colour = "black", size = 14, vjust= 2,  family= "book antiqua"),         plot.margin = unit(c(2,0,1,1),units="lines"))+       guides(colour = guide_legend(override.aes = list( size=3, linetype=0)))  ###bigger points in legend      ### extract data plot second y-axis ###      build = ggplot_build(ggplot_one)     breaks_gy <- build$panel$ranges[[1]]$y.labels     breaks_gy <- as.numeric(breaks_gy)                      ### points add labels     labels_gy <- linspace(min(xylim),max(xylim),length(breaks_gy))    ### linspace gives me labels     labels_gy <- round(labels_gy,digits=1)      ### second y axis ###      gplot.y <- ggplot(ggplot_data, aes(x = x1, y = dataset_second_yaxis_two)) +       geom_line(colour = "transparent") +       theme_classic()+       scale_y_continuous(breaks = breaks_gy, labels = labels_gy, expand = c(0,0),                          limits = c(round(xylim[1]-0.04*xylim[2],digits=2), round(xylim[2]+0.04*xylim[2], digits=2))) +       labs(y = "yaxis2") +       theme(axis.title.y = element_text( vjust=-8, hjust =0.5, family= "book antiqua",size = 14, color=colp[length(colp)]),               axis.text.y = element_text(hjust=1.7, family= "book antiqua",colour =colp[length(colp)]),                               ## reverse ticks go on other side             axis.ticks.length = unit(-0.15,"cm"),                             ## reverse spacing between ticks , text move text right             axis.ticks.margin = unit(-0.5, "cm"),              axis.title.x = element_blank(), ## remove x-axis attributes             axis.text.x = element_blank(),             axis.ticks.x = element_blank(),             axis.line.x = element_blank(),             plot.background = element_rect(fill = "transparent"),             plot.margin = unit(c(2,0,3.85,-2),units="lines"))           vp1 <- viewport(width = 0.9, height = 1, x = 0, y = 0.5, = c(0,0.5))         vp2 <- viewport(width = 0.1, height = 1, x = 0.9, y = 0.5,just = c(0,0.5))          print(ggplot_one,vp=vp1)         print(gplot.y ,vp=vp2) 

i searching whole day solution... hope can help


Comments