2015-06-24 331 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