简体   繁体   English

在R中多面ggplot2图的边距内按因数着色点

[英]Colouring points by factor within the margin of a faceted ggplot2 plot in R

I'd like to create a faceted plot with margins in ggplot2. 我想在ggplot2中使用边距创建多面图。 However, I'd like the margin plot to have colours according to from which facet the particular point has been derived. 但是,我希望边距图具有根据其派生特定点的面而变的颜色。 It's probably best illustrated with an example: 最好用一个例子来说明:

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(.~gear, margins = TRUE)

Within the margin plot labelled as "(all)", I want those dots that have "gear = 3" to be plotted with one colour, those with "gear = 4" with a second colour, and those with "gear = 5" with a third one. 在标记为“(all)”的边距图中,我希望将具有“ gear = 3”的点用一种颜色绘制,将“ gear = 4”的点用第二种颜色绘制,将“ gear = 5”的点进行绘制。与第三个。

This one doesn't do the job: 这个没有做:

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(col=gear))
p + facet_grid(.~gear, margins = TRUE)

Is there a way to achieve what I want? 有没有办法实现我想要的?

How about creating a new variable as a reference and colour the points by that? 如何创建一个新变量作为参考并以此为点着色? Seems to work provided you don't mind the points in the first 3 facets being coloured too. 如果您不介意前三个方面也被着色,这些点似乎可以正常工作。

mtcars$ref <- as.factor(mtcars$gear)

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(col=as.factor(gear)))
p + facet_grid(.~ref, margins = TRUE)

所有点按齿轮着色

EDIT: I have managed to get it to remove the colour key from the first 3 facets but not without playing around with the original data; 编辑:我设法使它从前3个方面中删除颜色键,但并非没有原始数据;

Duplicate the original data (so there are 2 of each record) then instead of using a margin plot to produce the "all" facet, using the redundant records instead. 复制原始数据(每个记录有2个),然后使用冗余记录而不是使用边距图来生成“所有”构面。

library(ggplot2)

mtcars$ref <- (mtcars$gear)

# create the duplicate
dat <- do.call("rbind", replicate(2, mtcars, simplify = FALSE)) 

# give the duplicates a false value for "gear" so they can be plotted together 
#This value can then be used for faceting, grouping everything with "all".

dat$ref[1:32] <- "all" 


# where not in the "all" facet, change "gear" to one (so they are plotted with the same colour)
dat$gear[dat$ref != "all"] <- 1

# then plot using ref as the facet and gear to colour points.

p <- ggplot(dat, aes(mpg, wt)) + geom_point(aes(col=as.factor(gear)))
p + facet_grid(.~ref, margins = F)

仅在最终构面中用齿轮着色的点

I'm not sure that's the best way to go about it but perhaps someone with more expertise might be able to advise? 我不确定这是否是最好的解决方法,但也许某个具有更多专业知识的人可能会提供建议?

Another option would be to create the faceted plots and margin plot separately and use the gridExtra library to combine them: 另一个选择是分别创建多面图和边距图,并使用gridExtra库将它们组合:

library(ggplot2)
library(gridExtra)
mtcars$ALL <- "all"
p <- ggplot(mtcars, aes(mpg, wt))
p1 <- p + geom_point() + facet_grid(.~gear)
p2 <- p + geom_point(aes(color=factor(gear))) + facet_grid(.~ALL)
grid.arrange(p1, p2, ncol=2)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM