You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中绘制特朗普与希拉里、奥巴马用词对比图遇ggplot报错求助

Fixing the object 'Donald Trump' not found Error in Your ggplot Word Frequency Plot

Hey there! Let's dig into why you're hitting that frustrating error in your word frequency comparison plot. The issue almost always boils down to how R handles column names with spaces in ggplot's non-standard evaluation system. Here's how to fix it step by step:

1. Double-Check Your Data Frame Columns

First, confirm that your frequency data frame actually has a column named exactly Donald Trump (capitalization, spaces, and all). Run this quick command to list all column names:

colnames(frequency)

Make sure "Donald Trump" appears in the output—typos or mismatched spacing are easy mistakes here.

2. Fix Column Name References in ggplot

ggplot can get confused by column names with spaces when using the standard aes syntax. You have two reliable fixes:

Option 1: Use Backticks Around the Column Name

Wrap Donald Trump in backticks everywhere you reference it in the aes call. This tells R to treat it as a single column name, not separate objects. Here's your revised code:

# Don't forget to load required packages first!
library(ggplot2)
library(scales)

ggplot(frequency, aes(x = proportion, y = `Donald Trump`, color = abs(`Donald Trump` - 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 = "Donald Trump", x = NULL)

Option 2: Use the .data Pronoun

For a more robust approach (especially if you're working with dynamic column names), use the .data pronoun to explicitly reference the column from your data frame:

ggplot(frequency, aes(x = proportion, y = .data[["Donald Trump"]], color = abs(.data[["Donald Trump"]] - proportion))) +
  # Rest of your plot code remains the same

3. Quick Additional Checks

  • Load the scales package: The percent_format() function lives here—if you haven't loaded it, you'll get a secondary error. Add library(scales) at the top of your script.
  • Verify your author column: Make sure this column contains the names of Hillary Clinton and Barack Obama (the two you're comparing to Trump) so facet_wrap() generates the correct panels.
  • Confirm proportion values: Ensure this column holds the normalized word frequency for each author-word pair, as expected from the tidytext workflow.

That should resolve the "object not found" error and get your comparison plot up and running!

内容的提问来源于stack exchange,提问作者A.joh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:47:42