在ggplot中绘制绘图之外的标签

时间:2017-03-08 20:59:59

标签: r ggplot2

我有一个由以下代码制作的情节:

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轴上)(即,我希望它说&#34; 5 4 3 2 1&# 34;在相应变量的水平(高度)的右侧垂直?

由于

1 个答案:

答案 0 :(得分:0)

如果你制作&#34;变量&#34; 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