如何更改ggplot的线宽? [英] How to change line width in ggplot?

查看:209
本文介绍了如何更改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)

# - -------------------------------------------------- ---------------------------------------
####食物的情节按行业划分的摩洛哥和土耳其安全指数
#------------------------------------- -------------------------------------------------- ---

#_Code_Begin ...

datamortur< - melt(ccfsirsts [ccfsirsts $ region %in%c(TUR,MAR),])#选择感兴趣的区域
datamortur1 < - datamortur [datamortur $ variable%in%c(pFSI2),]#选择食品安全(wht,gro,VegtFrut,osd,OthCrop,VegtOil,XPrFood)的利息指数
datamortur2< - datamortur1 [datamortur1 $ sector% ]#选择感兴趣的食品行业
datamortur3< - 子集(datamortur2,tradlib!=BASEDATA)#消除BASEDATA情景结果

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

图29< - ggplot(data = fsi.wht,aes (x = factor(sres),y = value,color = factor(tradlib)))
图29 + geom_line(aes(group = factor(tradlib),size = 2))+ facet_grid(regionsFull〜。,scale =free_y,labeller = reg_labeller)+ scale_colour_brewer(type =div)+
theme(axis.text.x = element_text(color ='black',angle = 90,size = 13,hjust = 0.5 ,vjust = 0.5),axis.title.x = element_blank())+
ylab(FSI(%Change))+ theme(axis.t (大小= 12,hjust = 0.5,vjust = 0.2))+
主题(strip.text.y = element_text(size = 11,hjust = 0.5,vjust = 0.5,face ='bold'))

我的结果:


Newresult with aes(size = 2):



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



best,
ismail

解决方案

尽管@Didzis具有正确答案,我将扩展几点

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



至于我可以告诉你想要的是在调用 aes() size 大小设置为单个值,而不是 map 当您调用 aes(size = 2)时,它会创建一个名为的变量。 $ b

`2` 并使用它来创建大小,将它从一个常量值映射为一个常量值l至 aes (因此它出现在您的图例中)。



使用size = 1(并且没有 reg_labeller 这可能是在脚本的某处定义的)

 图29 + 
geom_line(aes(group = factor(tradlib)),size = 1)+
facet_grid(regionsFull〜。,scales =free_y)+
scale_colour_brewer(type =div)+
主题(axis.text.x = element_text(
color ='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(color ='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

 图29 + 
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(color ='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(color ='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 '))

b $ b

Datalink: the data used

My code:

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'))

My result:

Newresult with aes(size=2):

My question: Is there a way to control for line width more precisely to avoid the result in the second plot? I particularly find it document-unfriendly, and more so for publishing purposes to include the plot with the newly defined line width.

best, ismail

解决方案

Whilst @Didzis has the correct answer, I will expand on a few points

Aesthetics can be set or mapped within a ggplot call.

  • An aesthetic defined within aes(...) is mapped from the data, and a legend created.

  • An aesthetic may also be set to a single value, by defining it outside aes().

As far as I can tell, what you want is to set size to a single value, not map within the call to aes()

When you call aes(size = 2) it creates a variable called `2` and uses that to create the size, mapping it from a constant value as it is within a call to aes (thus it appears in your legend).

Using size = 1 (and without reg_labeller which is perhaps defined somewhere in your script)

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'))

and with size = 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'))

You can now define the size to work appropriately with the final image size and device type.

这篇关于如何更改ggplot的线宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆