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

如何基于SQL查询结果用ggplot2生成词频图?R新手求助

Creating a Frequency Plot for AreaType with ggplot2

Hey there! As an R newbie, you're already off to a great start connecting to your SQL database and pulling data—nice work! Let's get that frequency plot for your AreaType column sorted out with ggplot2. I'll break this down into simple, actionable steps.

First: Make Sure Your Data is Ready

Before jumping into plotting, double-check that your df data frame actually contains the AreaType column you need. Run these quick checks to confirm:

# View the first few rows of your data
head(df)

# Check the structure of the data frame
str(df)

You should see AreaType listed as either a character or factor column here. If you notice missing values (NA), we can handle those in the next step.

Step 1: Calculate AreaType Frequencies

You have two easy ways to count how often each AreaType appears—pick whichever feels more intuitive to you:

Option 1: Using Base R (No Extra Packages Needed)

# Create a frequency table and convert it to a data frame
freq_table <- as.data.frame(table(df$AreaType, useNA = "no")) # useNA="no" excludes missing values
# Rename columns for clarity
colnames(freq_table) <- c("AreaType", "Frequency")

Option 2: Using dplyr (Cleaner Syntax, Requires Installing dplyr First)

If you don't have dplyr installed yet, run install.packages("dplyr") first, then:

library(dplyr)

# Count occurrences and store in a new data frame
freq_df <- df %>%
  count(AreaType, name = "Frequency", na.rm = TRUE) # na.rm=TRUE drops NA values

Step 2: Build Your Frequency Plot with ggplot2

Now that you have your frequency data, let's plot it. We'll use geom_col (for pre-calculated frequencies) or geom_bar (to let ggplot handle the counting directly).

Basic Frequency Bar Chart

This is the simplest version, using your pre-calculated frequency data:

library(ggplot2)

ggplot(freq_df, aes(x = AreaType, y = Frequency)) +
  geom_col(fill = "#2E86AB") + # Choose a fill color you like
  labs(
    title = "Frequency of Area Types",
    x = "Area Type",
    y = "Number of Occurrences"
  ) +
  theme_minimal() # Clean, modern theme

Improved Version: Sorted Bars & Readable Labels

If you have lots of AreaType categories, sorting the bars by frequency and rotating x-axis labels will make your plot much easier to read:

ggplot(freq_df, aes(x = reorder(AreaType, -Frequency), y = Frequency)) +
  geom_col(fill = "#2E86AB") +
  labs(
    title = "Frequency of Area Types (Sorted by Occurrence)",
    x = "Area Type",
    y = "Number of Occurrences"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1), # Rotate labels 45 degrees
    plot.title = element_text(hjust = 0.5) # Center the title
  )

Alternative: Plot Directly from Raw Data

If you don't want to pre-calculate frequencies, ggplot can do the counting for you with geom_bar:

ggplot(df, aes(x = AreaType)) +
  geom_bar(fill = "#2E86AB", na.rm = TRUE) + # stat="count" is default, so we don't need to write it
  labs(
    title = "Frequency of Area Types",
    x = "Area Type",
    y = "Number of Occurrences"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Quick Troubleshooting Tips

  • If your x-axis labels are overlapping: The axis.text.x tweak in the improved plot will fix that.
  • If you see NA in your plot: Make sure you added na.rm = TRUE in your counting step or in geom_bar.
  • If colors look off: Replace #2E86AB with any hex color code or named color (like "blue" or "green").

内容的提问来源于stack exchange,提问作者shmookh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:22:06