R语言cjoint包联合实验:按受访者特征分面绘图报错求助
Hey there! The error you're seeing happens because the plot() method for amce objects returns a base R plot, not a ggplot2 object. That means you can't use ggplot2's facet_grid() or facet_wrap() functions directly with it—those only work with ggplot objects.
Here are two solid ways to create faceted plots for your grouped AMCE results:
方法1:用cjoint原生工具生成多组图并组合
cjoint has a built-in multiplot() function that lets you combine multiple base R plots into one page. Here's how to use it:
- Split your data by the grouping variable (e.g.,
econ_class) - Calculate AMCEs for each group separately
- Generate a plot for each group
- Combine them into a single page
# 1. Split your dataset by economic class data_groups <- split(cdata_chosen, cdata_chosen$econ_class) # 2. Calculate AMCEs for each group amce_results <- lapply(data_groups, function(sub_df) { amce( chosen ~ gender + residence + party + partisanship + education + copartisan + profession_type + caste_type + pol_connection, data = sub_df, cluster = TRUE, respondent.id = "serial_id", design = "uniform" ) }) # 3. Create plots for each group group_plots <- lapply(names(amce_results), function(group) { plot( amce_results[[group]], main = paste("Candidate Preference -", group), xlab = "Change in E[Y]", text.size = 8, point.size = 0.3, dodge.size = 0.9 ) }) # 4. Combine plots into one page (adjust cols to control number of columns) multiplot(plotlist = group_plots, cols = 2)
方法2:提取AMCE数据并用ggplot2手动绘图(更灵活)
If you want full control over styling and faceting, extract the AMCE results into a data frame, then use ggplot2 to build your plot. This lets you use all ggplot2 features like facet_wrap() or facet_grid():
library(ggplot2) # Define a helper function to extract AMCE results with group labels get_group_amce <- function(sub_df, group_label) { amce_out <- amce( chosen ~ gender + residence + party + partisanship + education + copartisan + profession_type + caste_type + pol_connection, data = sub_df, cluster = TRUE, respondent.id = "serial_id", design = "uniform" ) # Convert AMCE results to data frame and add group info amce_df <- as.data.frame(amce_out$amce) amce_df$econ_class <- group_label return(amce_df) } # Apply helper function to all groups and combine results all_amce_data <- do.call(rbind, lapply(names(data_groups), function(g) { get_group_amce(data_groups[[g]], g) })) # Build faceted plot with ggplot2 ggplot(all_amce_data, aes(x = term, y = estimate, ymin = lower, ymax = upper)) + geom_pointrange(position = position_dodge(width = 0.9)) + coord_flip() + # Match the vertical orientation of cjoint's default plot facet_wrap(~ econ_class) + labs( title = "Overall Candidate Preference by Economic Class", x = "Candidate Feature", y = "Change in E[Y]" ) + theme_bw() + theme(axis.text.y = element_text(size = 8))
小提示
- Method 1 keeps the exact styling of cjoint's default plots, which is great if you want consistency with your original figure.
- Method 2 gives you full control over colors, fonts, and layout—perfect if you need to customize the plot for publications or presentations.
内容的提问来源于stack exchange,提问作者Haseeb

