geom_tile()如何处理重复数据?

时间:2017-07-25 19:01:46

标签: r ggplot2

如果特定的x和y位置有多个数据,geom_tile()如何组合数据?

示例:

library(ggplot2)
library(dplyr)

mtcars %>% 
ggplot(aes(x=vs,y=am,fill=mpg)) + 
geom_tile()

enter image description here

这不是意思:

library(ggplot2)
library(dplyr)

mtcars %>% 
group_by(vs,am) %>% 
summarize(mpg = mean(mpg)) %>% 
ggplot(aes(x=vs,y=am,fill=mpg)) + 
geom_tile()

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以看看:

library(ggplot2)
library(dplyr)

mtcars %>% 
  ggplot(aes(x=vs, y=am, fill=mpg)) + 
  geom_tile() -> gg

ggplot_build(gg) -> gb

gb$data[[1]]
##       fill x y PANEL group xmin xmax ymin ymax colour size linetype alpha
## 1  #30648F 0 1     1    -1 -0.5  0.5  0.5  1.5     NA  0.1        1    NA
## 2  #30648F 0 1     1    -1 -0.5  0.5  0.5  1.5     NA  0.1        1    NA
## 3  #356E9D 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA
## 4  #316692 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 5  #29577E 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 6  #275379 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 7  #1D3F5E 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 8  #3977A9 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 9  #356E9D 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 10 #2A5982 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 11 #275277 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 12 #234A6D 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 13 #254F73 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 14 #1F4464 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 15 #132B43 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 16 #132B43 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 17 #1E4161 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 18 #51A8EA 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA
## 19 #4B9BDA 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA
## 20 #56B1F7 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA
## 21 #316693 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 22 #204566 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 23 #1F4464 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 24 #1A3A57 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 25 #2A5982 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 26 #4288C1 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA
## 27 #3E81B6 0 1     1    -1 -0.5  0.5  0.5  1.5     NA  0.1        1    NA
## 28 #4B9BDA 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA
## 29 #214769 0 1     1    -1 -0.5  0.5  0.5  1.5     NA  0.1        1    NA
## 30 #2C5C85 0 1     1    -1 -0.5  0.5  0.5  1.5     NA  0.1        1    NA
## 31 #1F4363 0 1     1    -1 -0.5  0.5  0.5  1.5     NA  0.1        1    NA
## 32 #316692 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA

mtcars %>% 
  group_by(vs,am) %>% 
  summarize(mpg = mean(mpg)) %>% 
  ggplot(aes(x=vs,y=am,fill=mpg)) + 
  geom_tile() -> gg

ggplot_build(gg) -> gb

gb$data[[1]]
##      fill x y PANEL group xmin xmax ymin ymax colour size linetype alpha
## 1 #132B43 0 0     1    -1 -0.5  0.5 -0.5  0.5     NA  0.1        1    NA
## 2 #29577E 0 1     1    -1 -0.5  0.5  0.5  1.5     NA  0.1        1    NA
## 3 #2E608B 1 0     1    -1  0.5  1.5 -0.5  0.5     NA  0.1        1    NA
## 4 #56B1F7 1 1     1    -1  0.5  1.5  0.5  1.5     NA  0.1        1    NA