2017-03-08 106 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