使用 ggplot2 绘制差异
Plotting differences with ggplot2
我有一个这样的 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)
您的绘图最终为空,因为没有列 '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)
(我去掉了 facet wrap,因为你已经缩小到 Radicals。你可以把它加回去,然后 然后 做第一段代码,如果你做了 y=author和 facet_wrap(~author, ncol = 2)
基本上,tl:dr您的错误是由于尝试从变量而不是列创建轴造成的
如果您想做的是绘制一个图表,比较 x 轴上一个 "author"(比如保守党)和一个 "author"(也许是激进派)的频率在 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())
我有一个这样的 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)
您的绘图最终为空,因为没有列 '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)
(我去掉了 facet wrap,因为你已经缩小到 Radicals。你可以把它加回去,然后 然后 做第一段代码,如果你做了 y=author和 facet_wrap(~author, ncol = 2)
基本上,tl:dr您的错误是由于尝试从变量而不是列创建轴造成的
如果您想做的是绘制一个图表,比较 x 轴上一个 "author"(比如保守党)和一个 "author"(也许是激进派)的频率在 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())