如何在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

Newesult with aes(size = 2): NewResult-图

我的问题:是否有办法更精确地控制线宽以避免第二个图的结果? 我特别觉得它是文档不友好的,更重要的是出版的目的,包括新的定义线宽的情节。

最好,伊斯梅尔

虽然@Didzis有正确的答案 ,我将在几点上展开

可以在ggplot调用中设置或映射美学。

  • aes(…)中定义的美学是从数据映射而来的,并创build了一个图例。

  • 审美也可以通过在aes()之外定义一个单值来设定。

据我所知,你想要的是大小设置为单个值,而不是在调用aes()

当你调用aes(size = 2)它会创build一个名为`2`的variables,并使用它来创build大小,将其从一个常量值映射到一个调用aes (因此出现在您的图例中)。

使用size = 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')) 

在这里输入图像说明

和大小= 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')) 

在这里输入图像说明

您现在可以定义大小以适应最终的图像大小和设备types。

ggplot2中的ggplot2可以通过参数size= in geom_line()来改变。

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

在这里输入图像说明

    Interesting Posts