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

求助:在R中实现分类变量(X)与连续变量(Y)的可视化代码

Hey there! I totally get how frustrating it is to spend hours searching for the right R code without luck. Let's fix that—here are straightforward, customizable ways to visualize your interference level vs. percentage correct data, using both base R and the popular ggplot2 package.

First, let's make sure your data is in a tidy data frame (the easiest format for plotting in R). I'll include code to convert matrix/table data to a data frame too, just in case that's where you're stuck.

Step 1: Prepare Your Data

If you have multiple observations per interference level, use this example data frame (replace with your actual values):

# Example data with multiple observations per interference level
df <- data.frame(
  Interference = factor(c(4, 4, 20, 20, 40, 40, 60, 60)), # Treat as categorical factor
  Percentage_Correct = c(63.47, 65.21, 58.90, 56.72, 51.34, 49.87, 45.60, 43.29)
)

If you only have a single percentage value per interference level (e.g., a summary mean), use this:

# Example summary data (one value per interference level)
summary_df <- data.frame(
  Interference = factor(c(4, 20, 40, 60)),
  Percentage_Correct = c(63.47, 57.81, 50.60, 44.44)
)

If your data is in a matrix/table format:

# Convert matrix to tidy data frame
data_matrix <- matrix(c(63.47, 57.81, 50.60, 44.44), 
                      nrow = 1, 
                      dimnames = list("Percentage_Correct", c("4", "20", "40", "60")))

summary_df <- as.data.frame(t(data_matrix))
summary_df$Interference <- rownames(summary_df)
summary_df$Interference <- factor(summary_df$Interference) # Mark as categorical

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

Bar Plot (For Summary Means)

Great for showing single values per category:

barplot(summary_df$Percentage_Correct, 
        names.arg = summary_df$Interference,
        main = "Percentage Correct by Interference Level",
        xlab = "Interference Level",
        ylab = "Percentage Correct",
        col = "lightblue",
        ylim = c(0, 100)) # Y-axis 0-100 makes sense for percentages

Box Plot (For Multiple Observations)

Shows the distribution of correct percentages across each interference level:

boxplot(Percentage_Correct ~ Interference, data = df,
        main = "Distribution of Correct Percentage by Interference",
        xlab = "Interference Level",
        ylab = "Percentage Correct",
        col = "lightgreen",
        ylim = c(0, 100))

Option 2: ggplot2 (Polished, Customizable Plots)

ggplot2 is the go-to package for professional-looking graphs. First install it if you haven't already:

# Install once (uncomment if needed)
# install.packages("ggplot2")
library(ggplot2)

Bar Plot (Summary Means)

Clean, modern bar plot with easy customization:

ggplot(summary_df, aes(x = Interference, y = Percentage_Correct)) +
  geom_bar(stat = "identity", fill = "#2E86AB", alpha = 0.8) +
  labs(title = "Percentage Correct by Interference Level",
       x = "Interference Level",
       y = "Percentage Correct") +
  ylim(0, 100) +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5)) # Center the title

Box Plot + Scatter Points (For Multiple Observations)

Combines distribution (box plot) with individual data points to show full context:

ggplot(df, aes(x = Interference, y = Percentage_Correct)) +
  geom_boxplot(fill = "#F2D388", alpha = 0.6) +
  geom_jitter(width = 0.2, color = "#874C62", size = 2) + # Jitter avoids overlapping points
  labs(title = "Correct Percentage Distribution by Interference",
       x = "Interference Level",
       y = "Percentage Correct") +
  ylim(0, 100) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5))

All these code snippets are ready to run—just swap in your actual data values. Feel free to tweak colors, labels, or add extras like error bars if you need!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:20:15