ggplot2:修改散点图{ggplot2}中两个因素的图例元素? [英] ggplot2: modify legend's elements for two factors in scatterplot {ggplot2}?

查看:472
本文介绍了ggplot2:修改散点图{ggplot2}中两个因素的图例元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望散点图显示两个因素:按点大小和按灰色阴影:

I wish my scatter plot displays two factors: by point size and by shades of grey:

a1<-c(seq(1,10,1))
a2<-c(seq(11,20,1))
a3<-c(rep(c(1,2),each = 5))
a4<-c(rep(c(5,10,15,20,25),2))

df<-data.frame(a1,a2,a3,a4)

t1<-theme(        
  plot.background = element_blank(), 
  panel.grid.major = element_blank(), 
  panel.grid.minor = element_blank(), 
  panel.border = element_blank(), 
  panel.background = element_blank(),
  axis.line = element_line(size=.4))

ggplot(df, aes(x= a1, y= a2)) + 
  geom_point(aes(alpha=factor(a3), size = factor(a4))) + t1 + labs(x = "x label", y = "y label") +
  theme(legend.background = element_rect())

到目前为止,或多或少都很好.

So far, more or less good.

我的问题是:

  • 如何删除图例中的背景? theme(legend.background = element_rect())由于某种原因无法正常工作...
  • 如何同时修改我的图例标头?我想通过以下示例: http://www.cookbook-r.com/图/传奇_(ggplot2)/ 那应该是这样的:

  • how to remove the background in my legend? theme(legend.background = element_rect()) for some reason doesn't work...
  • how to modify both my legends headers? I imagine by this example : http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/ that is should be something like:

scale_shape_discrete(name ="modified A4", breaks=c("1", "2"), labels = c("one","two")),但我不知道如何使它工作?

scale_shape_discrete(name ="modified A4", breaks=c("1", "2"), labels = c("one","two")) but I can't figure that how to make it work?

我确定我完全误解了散点图中两个变量的显示,但是找不到解决方法?

I am sure that I am completely misunderstanding the displaying of two variables in scatter plot, but I can't find a way how to correct it?

谢谢!

推荐答案

基于@ user20650和@inscaven的建议以及更多的Google搜索,我希望更好地了解ggplot的组织方式以及如何制作我的图: >

Based on suggestions of @user20650 and @inscaven and on more google search I hope to understand better how the ggplot is organized and how to produce my plot:

# dummy data
a1<-c(seq(1,10,1))
a2<-c(seq(11,20,1))
a3<-c(rep(c(1,2),each = 5))
a4<-c(rep(c(5,10,15,20,25),2))

# create data frame 
df<-data.frame(a1,a2,a3,a4)

# set nice theme  
t1<-theme(        
  plot.background = element_blank(), 
  panel.grid.major = element_blank(), 
  panel.grid.minor = element_blank(), 
  panel.border = element_blank(), 
  panel.background = element_blank(),
  axis.line = element_line(size=.4))

# create scatter plot
ggplot(df, aes(x= a1, y= a2)) +                             # create basic plot
  geom_point(aes(size = factor(a4), colour = factor(a3))) + # colour has to be inside of aes !! (ASSIGNED = MAPPED)
  scale_colour_grey(name = "Set second\nline in title") +   # change title of 1st legend, change colours 
  scale_size_discrete(name = "Name by size") +              # change title of 2nd legend, size of point has been already assigned
  theme(legend.key = element_blank()) +                     # delete grey boxes around the legend
  labs(x = "x label", y = "y label") +                      # set labels on x and y  axes
  t1                                                        # add nice theme

其结果是:

这篇关于ggplot2:修改散点图{ggplot2}中两个因素的图例元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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