0

我有一个像这样的 R 数据框(名为frequency):

word    author  proportion
a   Radicals    1.679437e-04
aa  Radicals    2.099297e-04
aaa Radicals    2.099297e-05
abbe    Radicals    NA
aboow   Radicals    NA
about   Radicals    NA
abraos  Radicals    NA
ytterst Conservatives   5.581042e-06
yttersta    Conservatives   5.581042e-06
yttra   Conservatives   2.232417e-05
yttrandefrihet  Conservatives   5.581042e-06
yttrar  Conservatives   2.232417e-05

我想使用 ggplot2 绘制文档差异。像这样的东西

我有下面的代码,但我的情节最终是空的。

library(scales)
ggplot(frequency, aes(x = proportion, y = `Radicals`, color = abs(`Radicals` - proportion))) +
    geom_abline(color = "gray40", lty = 2) +
    geom_jitter(alpha = 0.1, size = 2.5, width = 0.3, height = 0.3) +
    geom_text(aes(label = word), check_overlap = TRUE, vjust = 1.5) +
  scale_x_log10(labels = percent_format()) +
  scale_y_log10(labels = percent_format()) +
  scale_color_gradient(limits = c(0, 0.001), low = "darkslategray4", high = "gray75") +
  facet_wrap(~author, ncol = 2) +
  theme(legend.position="none") +
  labs(y = "Radicals", x = NULL)
4

2 回答 2

2

您的情节最终为空,因为没有“激进分子”列。如果你试图缩小到只有激进分子然后绘制你应该做类似的事情

 radical_frequecy <- subset(frequency, author == 'Radicals')

那么你可以做

 library(scales)
 ggplot(radical_frequency, aes(x = proportion, y = author, color = abs(`Radicals` - proportion))) +
geom_abline(color = "gray40", lty = 2) +
geom_jitter(alpha = 0.1, size = 2.5, width = 0.3, height = 0.3) +
geom_text(aes(label = word), check_overlap = TRUE, vjust = 1.5) +
   scale_x_log10(labels = percent_format()) +
   scale_y_log10(labels = percent_format()) +
   scale_color_gradient(limits = c(0, 0.001), low = "darkslategray4", high = "gray75") +
   theme(legend.position="none") +
   labs(y = "Radicals", x = NULL)

(我取出了 facet wrap,因为您已经缩小到 Radicals。如果您执行了 y=author 和 facet_wrap(~author, ncol = 2),您可以将其重新添加,然后执行第一段代码

基本上,tl:dr 您的错误是由于尝试从变量而不是列创建轴引起的

于 2017-04-17T14:07:17.627 回答
1

如果您想要做的是绘制一个比较 x 轴上一位“作者”(例如,保守派)和 y 轴上一位“作者”(可能是激进派)的频率的图,您需要spread您的数据框(来自 tidyr 包),以便您可以以这种方式绘制它。

library(tidyverse)
library(scales)

frequency %>%
  spread(author, proportion) %>%
  ggplot(aes(Conservatives, Radicals)) +
  geom_abline(color = "gray40", lty = 2) +
  geom_point() + 
  geom_text(aes(label = word), check_overlap = TRUE, vjust = 1.5) +
  scale_x_log10(labels = percent_format()) +
  scale_y_log10(labels = percent_format())
于 2017-04-17T18:50:43.133 回答