如何在R中绘制半满点(最好使用ggplot) [英] How to draw half-filled points in R (preferably using ggplot)

查看:167
本文介绍了如何在R中绘制半满点(最好使用ggplot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以用多种颜色填充点。例如,在散点图中,我希望看到每个点的上半部分都填充了某种颜色,而下半部分填充了不同的颜色。如果可能的话,我还想在某些底层数据的每个点映射中使用填充颜色对(例如,如果数据取值为x的某些类型的配对)。到目前为止,我已经考虑过用两次日食两次阴谋并分别填写,但没有运气。如果有人能帮忙,我会很感激。



谢谢!

解决方案

以下是一些接近您要求的黑客行为:

首先我们用两个unicode符号来绘制上半部分和下半部分的圆圈。这些优点是每个点标记的中心是由每个半圆定义的圆的相互中心,但是它们各自包括圆的另一半的边界的缺点。结果,其中一个圆圈的轮廓覆盖另一个的边缘。您可以通过在其上绘制一个未填充的圆来覆盖圆形边框,但需要两个这样的未填充圆形,以便完全覆盖轮廓圆。另外,如果有任何点重叠,你会看到这些轮廓圈的一部分。 (理想情况下,会有unicode填充的半圆符号,没有标记的地理中心和(半)圆的中心重合的边界,但我一直找不到。)

  library(ggplot2)

p1 = ggplot(mtcars,aes(wt,mpg))+
geom_point(形状=\\\◒,color =red,size = 3)+
geom_point(shape =\\\◓,color =blue,size = 3)+
几何点(shape = 1,color =white,size = 3.05)+
geom_point(shape = 1,color =white,size = 2.8)+
theme_bw()

接下来我们绘制了两个用于半圆的unicode符号,其中另一半没有轮廓(我只能并排而不是上/下符号)。但是它们的缺点是标记的中心是标记的地理中心,而圆是偏离标记的中心。因此,您必须手工抵消这两个圈子,以便它们彼此排队。

  p2 = ggplot( mtcars)+ 
geom_point(aes(wt-0.027,mpg),shape =\\\◖,color =red,size = 3)+
geom_point(aes(wt + 0.027,mpg ),shape =\\\◗,color =blue,size = 3)+
theme_bw()

在上面的图中,我对颜色进行了硬编码,但您也可以将它们映射到其他变量。

要获取unicode符号为了正确显示,我使用了带有Symbola字体的Cairo PDF设备。

  cairo_pdf(p1.pdf,family = Symbola,4,4)
p1
dev.off()

cairo_pdf(p2.pdf,family =Symbola,4,4)
p2
dev.off()

下面是图表的样子:






I was wondering if it is possible to fill points with multiple colors. For example, in a scatter plot, I'd like to see for each point the upper half is filled with certain color while the lower half filled with a different one. If possible, I'd also like to make the pairs of filling colors in each point mappings from some underlying data (for instance, certain types of pairing for if the data takes value of x). So far I've thought about plotting twice with two eclipses and fill them separately, but there was no luck. I would really appreciate if someone could help.

Thanks!

解决方案

Here are a couple of hacks that get close to what you asked for:

First we plot with two unicode symbols for upper and lower semi-circles. These have the advantage that the center of each point marker is the mutual center of the circle defined by each semi-circle, but the disadvantage that they each include the border of the other half of the circle. As a result, the outline of one of the circles covers the edge of the other. You can "cover" the circle border by plotting an unfilled circle over it, but you need two such unfilled circles of slightly different sizes in order to completely cover the outline circle. In addition, if any points overlap, you'll see portions of these outline circles. (Ideally, there would be unicode filled semi-circle symbols without a border for which the geographic center of the marker and the center of the (semi-)circle coincide, but I haven't been able to find any.)

library(ggplot2)

p1 = ggplot(mtcars, aes(wt, mpg)) +
  geom_point(shape="\u25D2", colour="red", size=3) +
  geom_point(shape="\u25D3", colour="blue", size=3) +
  geom_point(shape=1, colour="white", size=3.05) +
  geom_point(shape=1, colour="white", size=2.8) +
  theme_bw()

Next we plot with two unicode symbols for semi-circles with no outline for the other half of the circle (I could only find side-by-side rather than upper/lower symbols). But these have the disadvantage that the center of the marker is the geographic center of the marker, while the circle is offset from the center of the marker. As a result, you have to offset the two circles by hand so that they line up against each other.

p2 = ggplot(mtcars) +
  geom_point(aes(wt-0.027, mpg), shape="\u25D6", colour="red", size=3) +
  geom_point(aes(wt+0.027, mpg), shape="\u25D7", colour="blue", size=3) +
  theme_bw()

In the plots above, I've hardcoded the colors, but you can map them to other variables as well.

To get the unicode symbols to display properly, I used the Cairo PDF device with the Symbola font.

cairo_pdf("p1.pdf", family="Symbola", 4,4)
p1
dev.off()

cairo_pdf("p2.pdf", family="Symbola", 4,4)
p2
dev.off()

Here's what the plots look like:

这篇关于如何在R中绘制半满点(最好使用ggplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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