0

如何用ggtern修改三元图

我使用以下代码绘制三元图,并希望进行一些更改:

  1. 将x、y、z的刻度标签修改为概率数(0.2 0.4 0.6 0.8 1.0)
  2. 删除 x、y、z 的侧箭头和相应的文本。
  3. 删除第二个图例(灰色/白色)
  4. 用“high”和“low”替换第一个图例中的文本
library(ggtern)
set.seed(1)
plot <- ggtern(data = data.frame(x = runif(100),
                                 y = runif(100),
                                 z = runif(100)),
               aes(x, y, z))

plot + stat_density_tern(geom = 'polygon',
                         n         = 400,
                         aes(fill  = ..level..,
                             alpha = ..level..)) +
  geom_point() +
  theme_rgbg() +
  theme(legend.justification=c(0,1), legend.position=c(0,1)) +
  #theme_gridsontop() + 
  labs(title = "Example Density/Contour Plot")    +
  scale_fill_gradient(low = "blue",high = "red")  +
  # scale_color_gradient(low="yellow",high="red") + 
  guides(fill = guide_colorbar(order=1),color="none")

在此处输入图像描述

4

1 回答 1

0

尝试以下修改:

  1. 刻度标签由labels = 中使用的三个连续刻度中的每一个中的参数控制ggtern。这些scale_L_continuous用于左侧边缘、scale_R_continuous右侧边缘和scale_T_continuous底部边缘。将中断设置为c(0, 0.2, 0.4, 0.6, 0.8, 1)(或更简洁地使用0:5 / 5),并设置labels为相同的值。
  2. theme_noarrows()您可以通过添加到绘图来删除箭头。
  3. 删除 alpha 图例(灰色和白色的)使用guides(alpha = guide_none())
  4. 您可以通过labels作为参数传递给scale_fill_gradient. 由于有 5 个中断 (1:5),我们传递了五个标签:c("low", "", "", "", "high")

这是一个完整的代表。如果您在新的 R 会话中完全剪切并粘贴此代码,您应该得到完全相同的图:

library(ggtern)
set.seed(1)

ggtern(data = data.frame(x = runif(100), y = runif(100), z = runif(100)),
       mapping = aes(x, y, z = z)) +
  stat_density_tern(geom = 'polygon', n = 400,
                    aes(fill  = ..level.., alpha = ..level..)) +
  geom_point() +
  scale_fill_gradient(low = "blue", high = "red", name = "", breaks = 1:5, 
                      labels = c("low", "", "", "", "high"))  +
  scale_L_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
  scale_R_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
  scale_T_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
  labs(title = "Example Density/Contour Plot") +
  guides(fill = guide_colorbar(order = 1), alpha = guide_none()) +
  theme_rgbg() +
  theme_noarrows() +
  theme(legend.justification = c(0, 1), 
        legend.position      = c(0, 1))

reprex 包(v0.3.0)于 2020 年 11 月 24 日创建

于 2020-11-24T13:28:34.423 回答