2013-02-10 39 views
61

數據鏈接: the data used如何更改ggplot中的線寬?

我的代碼:

ccfsisims <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_ConsIndex.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE) 
ccfsirsts <- as.data.frame(ccfsisims) 
ccfsirsts[6:24] <- sapply(ccfsirsts[6:24],as.numeric) 
ccfsirsts <- droplevels(ccfsirsts) 
ccfsirsts <- transform(ccfsirsts,sres=factor(sres,levels=unique(sres))) 

library(ggplot2) 

#------------------------------------------------------------------------------------------ 
#### Plot of food security index for Morocco and Turkey by sector 
#------------------------------------------------------------------------------------------ 

#_Code_Begin... 

datamortur <- melt(ccfsirsts[ccfsirsts$region %in% c("TUR","MAR"), ]) # Selecting regions of interest 
datamortur1 <- datamortur[datamortur$variable %in% c("pFSI2"), ] # Selecting the food security index of interest 
datamortur2 <- datamortur1[datamortur1$sector %in% c("wht","gro","VegtFrut","osd","OthCrop","VegtOil","XPrFood"), ] # Selecting food sectors of interest 
datamortur3 <- subset(datamortur2, tradlib !="BASEDATA") # Eliminating the "BASEDATA" scenario results 

allfsi.f <- datamortur3 
fsi.wht <- allfsi.f[allfsi.f$sector %in% c("wht"), ] 

Figure29 <- ggplot(data=fsi.wht, aes(x=factor(sres),y=value,colour=factor(tradlib))) 
Figure29 + geom_line(aes(group=factor(tradlib),size=2)) + facet_grid(regionsFull~., scales="free_y", labeller=reg_labeller) + scale_colour_brewer(type = "div") + 
theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 13, hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) + 
ylab("FSI (%Change)") + theme(axis.text.y = element_text(colour = 'black', size = 12), axis.title.y = element_text(size = 12, hjust = 0.5, vjust = 0.2)) + 
theme(strip.text.y = element_text(size = 11, hjust = 0.5, vjust = 0.5, face = 'bold')) 

我的結果: Result_Figure

Newresult用AES(大小= 2): NewResult-Figure

我的問題: 是否有一種更精確地控制線條寬度以避免結果的方法第二個陰謀?我特別發現它對文檔不友好,而且對於發佈目的而言,包括新定義的線寬的情節更是如此。

最好, 伊斯梅爾在ggplot2

+8

要更改線寬,只需將參數大小= 2添加到geom_line()。 – 2013-02-10 06:44:56

+2

剛剛做了一些實驗,看起來大小不必像1和2一樣使用整數值。我剛剛進入1.5並在中間找到了一些東西。我不確定在任何情況下,這樣的固定值是否適用於您,但至少可以調整。 – jxramos 2014-10-30 20:10:38

回答

77

雖然@Didzis有correct answer,我將擴大在幾個點

美學可以設置或ggplot調用中映射。

  • aes(...)中定義的美學是從數據映射而來的,並創建了一個圖例。

  • 通過在aes()之外定義審美,也可以將審美設置爲單個值。

據我所知,你想要的是設置大小爲單個值,而不是地圖呼叫內aes()

當你調用aes(size = 2)它創建了一個名爲變量`2`並使用它來創建大小,因爲它在調用aes(因此它出現在您的圖例中)從一個常量值映射它。

使用大小= 1(和不reg_labeller這也許是在腳本中的某處定義的)

Figure29 + 
    geom_line(aes(group=factor(tradlib)),size=1) + 
    facet_grid(regionsFull~., scales="free_y") + 
    scale_colour_brewer(type = "div") + 
    theme(axis.text.x = element_text(
      colour = 'black', angle = 90, size = 13, 
      hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) + 
    ylab("FSI (%Change)") + 
    theme(axis.text.y = element_text(colour = 'black', size = 12), 
      axis.title.y = element_text(size = 12, 
      hjust = 0.5, vjust = 0.2)) + 
    theme(strip.text.y = element_text(size = 11, hjust = 0.5, 
      vjust = 0.5, face = 'bold')) 

enter image description here

並用大小= 2

Figure29 + 
    geom_line(aes(group=factor(tradlib)),size=2) + 
    facet_grid(regionsFull~., scales="free_y") + 
    scale_colour_brewer(type = "div") + 
    theme(axis.text.x = element_text(colour = 'black', angle = 90, 
      size = 13, hjust = 0.5, vjust = 
      0.5),axis.title.x=element_blank()) + 
    ylab("FSI (%Change)") + 
    theme(axis.text.y = element_text(colour = 'black', size = 12), 
      axis.title.y = element_text(size = 12, 
      hjust = 0.5, vjust = 0.2)) + 
     theme(strip.text.y = element_text(size = 11, hjust = 0.5, 
      vjust = 0.5, face = 'bold')) 

enter image description here

你現在可以定義e尺寸與最終的圖像尺寸和設備類型相適應。

43

線寬可以與參數size=geom_line()被改變。

#sample data 
df<-data.frame(x=rnorm(100),y=rnorm(100)) 
ggplot(df,aes(x=x,y=y))+geom_line(size=2) 

enter image description here

+2

我已經嘗試過您的選擇,但問題是該行變得太厚而不能被視爲文檔友好。當使用aes(大小)時,我得到了我添加的第二個圖。我希望能夠以更系統的方式控制寬度,以便選擇最合適的方式。 – iouraich 2013-02-10 20:37:37

+5

@ smailov83,不要在調用'aes'的時候放大小。請參閱我的回答(或者'ggplot2'書中的解釋。 – mnel 2013-02-11 00:43:36

+3

默認值似乎小於'size = 1',也許是'0.5',所以在我看來,使用size = 1會產生相當不錯的結果。也使用十進制數字來微調寬度(比如'size = 1.2')。 – Ricardo 2016-12-29 19:03:05