如何在R中绘制半填充点(最好使用ggplot)

时间:2023-02-09 21:13:26

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.

我想知道是否可以用多种颜色填充点。例如,在散点图中,我想看到每个点上半部分都填充了某种颜色,而下半部分填充了不同的颜色。如果可能的话,我还想在一些基础数据的每个点映射中建立填充颜色对(例如,如果数据取值为x,则为某些类型的配对)。到目前为止,我已经考虑过用两次日食绘制两次并分别填充,但没有运气。如果有人能提供帮助我真的很感激。

Thanks!

谢谢!

1 个解决方案

#1


6  

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

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

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.

接下来,我们用两个unicode符号绘制半圆形,没有轮廓的另一半圆(我只能找到并排而不是上/下符号)。但是这些缺点是标记的中心是标记的地理中心,而圆偏离标记的中心。因此,您必须手动偏移两个圆圈,以便它们相互对齐。

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.

为了正确显示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()

Here's what the plots look like:

这是情节的样子:

如何在R中绘制半填充点(最好使用ggplot)

如何在R中绘制半填充点(最好使用ggplot)

#1


6  

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

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

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.

接下来,我们用两个unicode符号绘制半圆形,没有轮廓的另一半圆(我只能找到并排而不是上/下符号)。但是这些缺点是标记的中心是标记的地理中心,而圆偏离标记的中心。因此,您必须手动偏移两个圆圈,以便它们相互对齐。

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.

为了正确显示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()

Here's what the plots look like:

这是情节的样子:

如何在R中绘制半填充点(最好使用ggplot)

如何在R中绘制半填充点(最好使用ggplot)