r - 如何改变ggplot中的线宽?

标签 r ggplot2 line-plot

数据链: the data used

我的代码:

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

aes 的新结果(大小=2): NewResult-Figure

我的问题: 有没有办法更精确地控制线宽以避免第二个图中的结果?我特别发现它对文档不友好,尤其是出于发布目的,包括具有新定义的线宽的绘图。

最好的, 伊斯梅尔

最佳答案

虽然 @Didzis 有 correct answer ,我再扩展一下几点

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

  • aes(...) 中定义的美学是从数据映射出来的,并创建了图例。

  • 美学也可以通过在 aes() 外部定义来设置为单个值。

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

当您调用 aes(size = 2) 时,它会创建一个名为 `2` 的变量,并使用它来创建大小,将其从常量值映射为位于对 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'))

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

您现在可以定义尺寸以适合最终图像尺寸和设备类型。

关于r - 如何改变ggplot中的线宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14794599/

相关文章:

r - 每个点而不是单个数据点的散点图汇总统计数据(例如总和或平均值)

r 编程 --- twitteR OAuthFactory 对象错误

r - 如何使用管道和 purrr 获取列表名称和切片名称

r - 默认情况下,在 R 中使用 ggplot 的同名美学?

r - 如何使用 Shiny 应用程序中的 react 数据为 ggplotly 中的每个数据类别设置固定线条颜色?

R使用列索引号预测数据框中每一列的glm拟合

R 将等高线保存到数据框中

r - 如何绘制以 x 轴为因子的平滑线 (ggalt::xspline) 图

r - ggplot 线的选择性标记

x 轴上带有日期的 seaborn 线图