微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过多种美学方式更改图例大小/如何在R中的图例中的变量之间增加空间

如何解决如何通过多种美学方式更改图例大小/如何在R中的图例中的变量之间增加空间

我的问题分为两个部分。下面提供了示例数据和使用ggplot创建代表性图的代码

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources androidx.fragment.app.FragmentActivity.getResources()' on a null object reference
    at com.devqx.covoiturage.Ride.Home.FragmentAllRides$GetAllRides.onPostExecute(FragmentAllRides.java:672)
    at com.devqx.covoiturage.Ride.Home.FragmentAllRides$GetAllRides.onPostExecute(FragmentAllRides.java:464)
    at android.os.AsyncTask.finish(AsyncTask.java:755)
structure(list(x = c(2.93131952459005,3.21275054434318,1.36466997175509,2.13626543532502,1.45889556823722,1.94598707699052,0.719062322132357,2.38139571953234,2.37813367615963,3.98126576880209),y = c(7.51581380181603,9.77495763943671,8.9666894018554,8.62675858853528,7.89238665417542,9.84865061237773,7.24526820962333,7.64727218939944,7.28026738945878,8.6913070524479),z = structure(c(1L,1L,2L,3L,3L),.Label = c("a","b","c"),class = "factor"),z2 = structure(c(1L,2L),.Label = c("cat","dog"),class = "factor")),class = "data.frame",row.names = c(NA,-10L))

第1部分:在存在多种美学的情况下更改图例的大小。

当您查看生成的图形时,图例中的项目z具有两种关联的美学:颜色和形状。但是,它指的是线条颜色,而不仅仅是点颜色/形状。我想知道是否有办法增加z的点的大小(仅在图例中),同时保持z的线大小不变。我尝试使用ggplot(exampledata,aes(x = x,y = y,color = z,shape = z))+geom_point()+geom_line(aes(color = z,linetype = z2))+ scale_linetype_manual(values = c(1,2,3))+theme(legend.position = 'top') 作为参数来使用大小,但这会增加点和线的大小。

第2部分:增加图例中变量之间的空间。

这应该很简单,但是我还没有找到一个简单的答案。在图例中,z和z2显然是独立的组件,但是是否有办法增加这些变量之间的整体距离?我并不是说要像override.aes()中那样增加每个变量的每个级别之间的空间。本质上,我希望z的“ c”和z2的标题之间有更多的空格。

解决方法

要回答您的第1部分问题,请参阅filups21对以下SO问题ggplot2: Adjust the symbol size in legends的答复。本质上,您需要使用以下代码来更改基本的网格美感。

# Make JUST the legend points larger without changing the size of the legend lines:
# To get a list of the names of all the grobs in the ggplot
g
grid::grid.ls(grid::grid.force())

# Set the size of the point in the legend to 2 mm
grid::grid.gedit("key-[-0-9]-1-1",size = unit(4,"mm"))

# save the modified plot to an object
g2 <- grid::grid.grab()
ggsave(g2,filename = 'g2.png')

第2部分的答案要简单一些,只需要在ggplot2语句中添加少量内容即可。尝试添加以下内容:+ theme(legend.spacing = unit(5,units = "cm"))。然后进行单位测量,直到获得所需的外观为止。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。