使用ggplot2将图例中的线宽与图中的线宽相匹配 [英] Matching line width in legend to line width in plot using ggplot2

查看:830
本文介绍了使用ggplot2将图例中的线宽与图中的线宽相匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是一个



请注意,上面的命令作用于一个数据帧,该数据帧被扩展到使用dput()发布的原始数据帧。

上一篇文章的全部目的是找出如何在繁忙的情节中突出显示两行。然而,这种清晰度除非图例还显示了指定厚度的关联线,否则目标会减小。我不想用太多的传说来混淆情节,而目前,传奇的配方和Fa.IVIVC似乎已经足够。 因此,我真的希望变量Highlight= TRUE的行的线宽显示在图例Fa.IVIVC中。如何做到这一点?



谢谢。

解决方案

您甚至可以更多地手动定义 scale_size_manual ...

中的行宽度。我认为稍微修改图例关键字删除灰色背景(使其与主题匹配),并使用 theme <中的 legend.key.width 参数增加该键的宽度。 / code>(否则你的粗虚线的图例只显示一个点)。

pre $ g $ p $ ggplot()+
geom_point(data = df,aes(
x = invitro,
y = invivo,
color = factor(Form,labels = c(Fast,Medium,Slow ))
))+
geom_line(
data = line_data,
aes(x = invitro,y = Fabs,linetype =`Fa.IVIVC`,size =`Fa .IVIVC`)
)+
labs(title =Plot,color =Formulation)+
scale_x_continuous(limits = c(0,100))+
scale_y_contin (限制= c(0,100))+
scale_size_manual(values = c(DWeibull= 0.5,Fa = Fd= 2,
Fa = m * Fd + c= 0.5,polyx2= 0.5,polyx3= 0.5,
powerlaw= 2,Sigmoid= 0.5,SWeibull= 0.5,
时间比例1= 0.5,比例尺2= 0.5))+
主题(
panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(color =black),
legend.key = element_blank(),legend.key.width = unit(4,line)


This question is a follow-up to a previous question I posted to stackoverflow which has already been addressed. I wish to thank once again the respondent who took time to address the previous question. For the sake of conciseness, I will not reproduce the details of the previous question, so please refer to the hyperlink to view the data set in order to reproduce the problem.

In summary, I have a plot involving three sets of points grouped by color according to a column "Formulation", and eight non-colored lines grouped using linetype and size, the latter being mapped to two different grouping variables ("Fa.IVIVC" and "Highlight" respectively). The command used is reproduced below followed by the resulting plot.

> ggplot() +
+   geom_point(data = df, aes(
+     x = invitro,
+     y = invivo,
+     colour = factor(Form, labels = c("Fast", "Medium", "Slow"))
+   )) +
+   geom_line(
+     data = line_data,
+     aes(x = invitro, y = Fabs, linetype = `Fa.IVIVC`, size = Highlight)
+   ) +
+   labs(title = "Plot", colour = "Formulation") +
+   scale_x_continuous(limits = c(0, 100)) +
+   scale_y_continuous(limits = c(0, 100)) +
+   guides(size = FALSE) +
+   scale_size_manual(values = c("TRUE" = 2, "FALSE" = 0.5)) +
+   theme(
+     panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
+     panel.background = element_blank(), axis.line = element_line(colour = "black")
+   )

Note that the above command acts on a data frame which was augmented to the original one I posted using dput().

The whole purpose of the previous post was to find out how to highlight two lines in an otherwise busy plot. However, the clarity of this objective is diminished unless the legend also shows the associated lines in the specified thicknesses. I do not want to clutter the plot with too many legends, and currently, the legends 'Formulation' and 'Fa.IVIVC' appear to be sufficient. So, I really want the linewidths of the lines where the variable "Highlight" = TRUE to show up in the legend 'Fa.IVIVC'. How can this be done?

Thank you.

解决方案

You can define even more manually the line widths in scale_size_manual...
I think it is better also to slightly modify the legend key to remove the gray background (to make it match you theme) and to increase the width of the key with the legend.key.width argument in theme (otherwise you the legend of the thick dotted line show only one dot).

ggplot() +
    geom_point(data = df, aes(
        x = invitro,
        y = invivo,
        colour = factor(Form, labels = c("Fast", "Medium", "Slow"))
        )) +
    geom_line(
        data = line_data,
        aes(x = invitro, y = Fabs, linetype = `Fa.IVIVC`, size = `Fa.IVIVC`)
        ) +
    labs(title = "Plot", colour = "Formulation") +
    scale_x_continuous(limits = c(0, 100)) +
    scale_y_continuous(limits = c(0, 100)) +
    scale_size_manual(values = c("DWeibull" = 0.5, "Fa = Fd" = 2, 
                                 "Fa = m*Fd + c" = 0.5, "polyx2" = 0.5, "polyx3" = 0.5, 
                                 "powerlaw" = 2, "Sigmoid" = 0.5, "SWeibull" = 0.5, 
                                 "Time scale 1" = 0.5, "Time scale 2" = 0.5)) +
    theme(
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank(), legend.key.width = unit(4,"line")
        ) 

这篇关于使用ggplot2将图例中的线宽与图中的线宽相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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