用ggplot2绘制差异

时间:2017-04-17 13:58:14

标签: r plot ggplot2 tidyverse tidytext

我有一个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绘制文档差异。像this

这样的东西

我有下面的代码,但我的情节结果为空。

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)

2 个答案:

答案 0 :(得分:2)

你的情节结果为空,因为没有一个列'Radicals'。如果你试图缩小到只有Radicals,然后绘制你应该做的事情

 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)

(因为你已经缩小到Radicals,我拿出了facet wrap。你可以把它添加回来然后然后做第一段代码如果你做了y = author和facet_wrap(〜作者) ,ncol = 2)

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

引起的

答案 1 :(得分:1)

如果您想要做的是制作一个比较一个&#34;作者&#34;的频率的情节。 (比如说,保守党)在x轴和一个&#34;作者&#34; (可能是Radicals)在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())