簡體   English   中英

ggplot2:修改散點圖{ggplot2}中兩個因素的圖例元素?

[英]ggplot2: modify legend's elements for two factors in scatterplot {ggplot2}?

我希望散點圖顯示兩個因素:按點大小和按灰色陰影顯示

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

到目前為止,或多或少都很好。

我的問題是:

  • 如何刪除我的圖例中的背景? theme(legend.background = element_rect())由於某種原因不起作用...
  • 如何修改我的兩個圖例標頭? 我想通過這個例子: http : //www.cookbook-r.com/Graphs/Legends_(ggplot2 )/應該是這樣的:

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

我確定我完全誤解了散點圖中兩個變量的顯示,但是我找不到解決方法?

謝謝 !

在此處輸入圖片說明

基於@ user20650和@inscaven的建議以及更多的Google搜索,我希望更好地了解ggplot的組織方式以及如何生成我的圖:

# 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

結果是:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM