2017-03-08 157 views
0

我从下面的代码做了一个情节:在ggplot情节之外绘制标签

variable=c("A","B","C","D","E") 
value=c(1,2,3,4,5); 
type=c("A","B","A","A","B") 
temp<-data.frame(var=factor(variable),val=value,type=factor(type)) 
p<-ggplot(temp,aes(var,val,color=type))+geom_point(aes(colour="type")) 
p<-p+coord_flip()+theme(plot.margin = unit(c(1,5,1,1), "lines"),legend.position = "none") 

myplot

如何为情节上的值(现在x轴)的标签在正确的电平(即,图的右侧,我希望它在相应的变量的水平(高度)说“5 4 3 2 1”竖直右侧?

由于

回答

0

,如果你使“变量” y轴的标签,而不是情节的实际值,可以使用sec_axis为1:1转换:

temp <- data.frame(val = value, var = value, type = type) 
p <- ggplot(temp,aes(var,val,color=type)) + 
    geom_point(aes(colour="type")) + 
    theme(plot.margin = unit(c(1,5,1,1), "lines"), legend.position = "none") 
p <- p + scale_y_continuous(labels = variable, sec.axis = sec_axis(~.*1)) 
p 

enter image description here