2015-06-24 369 views
1

我使用ggplot2在r中组成了一个图。代码如下。我的数据集是net_d。比率在0.5 - 1.0之间变化。 D2变为从10 - 1ggplot x轴刻度标记标签

p<-ggplot(net_d,aes(Ratio,D2)) 
p<-p+geom_point()+geom_line() 
p<-p+xlab("Multiples of degrees of nodes within community to between community") 
p<-p+ylab("Faction of vertices classfied correctly") 
#p<-p+scale_x_continuous(breaks=c(seq(10,1,by=-1))) 
p<-p+xlim(10,1)+ylim(0.5,1) 
p<-p+theme_set(theme_grey()) 
print(p) 

enter image description here

我需要有沿x轴10的标签 - 1,通过1步骤因此,在情节的每个点将具有在x处的相应刻度标记-轴。我有评论可能的解决方案。但是,将标签逆转为1-10。我需要其他方式。

回答

1

这应该工作:

library(ggplot2) 
coords <- rev(seq(1:10)) 
vals <- c(rep(1,6),0.98,0.73,0.64,0.66) #approximately extracted from your graph 
df <- as.data.frame(rbind(coords,vals)) 
p <- ggplot(df,aes(x=coords,y=vals)) 
p <- p + geom_point() + geom_line() 
p <- p + xlab("Multiples of degrees of nodes within community to between community") 
p <- p + ylab("Faction of vertices classfied correctly") 
p <- p + theme_bw() 
p <- p + ylim(0.5,1) 
p <- p + scale_x_reverse(breaks=c(seq(1,10))) 

下面是输出: enter image description here

希望这有助于

+0

'scale_x_reverse'是关键 – akalanka

0

您可以使用scale_x_reverse

#using the cars database 
p<-ggplot(cars,aes(speed,dist)) 
p<-p+geom_point()+geom_line() 
p<-p+xlab("Multiples of degrees of nodes within community to between community") 
p<-p+ylab("Faction of vertices classfied correctly") 
p<-p+scale_x_reverse(breaks=c(seq(1,25,by=1))) 
p<-p+ylim(1,100) 
p<-p+theme_set(theme_grey()) 
print(p) 

enter image description here

+0

它运行良好。你有什么想法为什么我的传奇标题不会随着p <-p + guides(fill = guide_legend(title =“Dimension”))'而改变。它将原始列名称保留为标题 – akalanka