2014-09-06 233 views
0

这里是我的数据(称为“数据”,是一个CSV格式文件):在ggplot2中,如何在图中添加另一个y轴?

attitude,order,min,max,mean,SpRate 
Commanding,7,0.023005096,1.6517,0.681777825,5.66572238 
Friendly,10,0.20565908,1.7535,0.843770095,6.191950464 
Hostile,12,0.105828885,2.4161,1.128603777,6.493506494 
Insincere,1,0.110689225,1.5551,0.730545923,5.115089514 
Irony,4,0.089307133,2.2395,0.955312553,5.249343832 
Joking,2,0.165717303,2.1871,0.94512688,5.141388175 
Neutral,5,-0.044620705,1.5322,0.696879247,5.420054201 
Polite,11,0.170151929,1.8467,0.873735105,6.191950464 
Praising,8,0.192402573,2.0631,0.972857404,5.797101449 
Rude,13,0.249746688,2.2885,1.100819511,6.644518272 
Serious,6,0.011312206,1.7195,0.693606814,5.649717514 
Sincere,9,-0.09135461,1.6409,0.659525513,5.813953488 
Suggesting,3,0.072541529,1.8345,0.82999014,5.249343832 

这里是我的代码:

library(ggplot2) 
ggplot (data, aes(x=order))+ 
    geom_rect(aes(xmin=order-0.1, xmax=order+0.1, ymin = min, ymax=max), size=1, alpha=0,color="black")+ 
    geom_bar(aes(y=SpRate, fill="SpRate"),stat="identity", alpha=0.2, width=0.9)+ 
    geom_point(aes(y=min, shape="min"), size=5, fill="white")+ 
    geom_point(aes(y=mean, shape="mean"), size=5)+ 
    geom_point(aes(y=max, shape="max"), size=5)+ 
    scale_x_continuous(breaks=c(1:13), labels=c("Insincere","Joking","Suggesting","Irony","Neutral","Serious","Commanding","Praising","Sincere","Friendly","Polite","Hostile", "Rude"))+ 
    xlab("")+ylab("")+theme_bw()+ 
    theme(axis.text.x=element_text(size=25,angle=45, vjust=0.5, color="black"))+ 
    theme(legend.text = element_text(size = 20))+ 
    theme(legend.title = element_text(size = 20))+ 
    labs(shape = "f0:", fill = "SpRate:")+ 
    scale_shape_manual(values=c("min"=15,"max"=16,"mean"=18))+ 
    scale_fill_manual(values= "black")+ 
    theme(axis.text.y = element_text(size=20)) 

所以,你可以从图中看到,有实际上有两个图:一个带有点和条形图的矩形区域,但是条形图的y轴显然不适应y轴,因此,如何在整个图的右侧添加另一个y轴这可以更好地调整条形图吗? (即我想要矩形的y轴呈现从0到2.5,条形图从0到7)

+2

次轴不受GGPLOT2支持不会被使用。 – Roland 2014-09-06 11:26:15

回答