1

我在使用 cowplot 包中的 plot_grid 函数对我的地块进行大小调整和对齐时遇到问题。左下角的情节似乎总是比其他情节小一点。我做了一些研究,似乎找不到任何有效的方法。我是 R 的新手,所以任何帮助将不胜感激!谢谢!

附件是我的代码以及情节的样子和我希望它的样子

'#Data frame with huc results for each parameter
parameter_results <- readRDS("param_results_2014.RDS") %>% select(1:84)
#list of parameter names
parameters <- sort(readRDS("parameters.RDS"))

 blank_theme <- theme_minimal()+ theme(
  axis.title.x = element_blank(),
  plot.margin = unit(c(0,0,0,0), "pt"),
  axis.title.y = element_blank(),
  panel.border = element_blank(),
  legend.position=c(.5,.02),
  legend.direction="horizontal",
  legend.key=element_rect(colour="black",size=0.5,linetype="solid"),
  panel.grid=element_blank(),
  axis.ticks = element_blank(),
  plot.title= element_text(size=8, vjust=-4.0, face="bold")
)


#Function for creating poroportions table for parameters
parameter_summary <-function(parameter) {
  parameter_df <- parameter_results %>%
    select(results = parameter) %>%  #keep only column for the parameter you want to plot
    filter(results != "Not Applicable") %>%  #filters out 'not applicable' results
    count(results) %>%               #
    mutate(prop = prop.table(n), perc = paste0(round(prop * 100),"%"))
  return(parameter_df)
}


parameter_pie_chart <- function(parameter,title="",nudgex=5,nudgey=-10) {
  # parameter: the parameter you want to create a pie chart for, example: 'DO'
  # title: plot title, default is the name of the parameter

  parameter_df <- parameter_summary(parameter) 
  #data frame of proportions less than 10%. necessary because for these values, labels are implemented with an arrow
  small_perc <- parameter_df %>% filter(prop < .10)
  #dataframe of proportions greater than 10%
  signif_perc <- parameter_df %>% filter(prop >= .10)

  pie_chart <- ggplot(parameter_df, aes(x = "", y = n, fill = fct_inorder(results))) +
    geom_bar(stat = "identity", width = 1,colour='black') + 
    coord_polar(theta = "y") +
    blank_theme +
    theme(axis.text.x=element_blank()) + 
    theme(legend.title=element_blank()) +
    #ggtitle(title)+
    theme(plot.title = element_text(hjust = 0.5))   +
     geom_text(data = signif_perc, aes(label = perc), 
                position = position_stack(vjust = .5), size = 5, show.legend = F) +
    scale_fill_manual(values = c("Attaining" = "#99FF99","Insufficient Information" = "#FFFF99", "Non Attaining" = "#FF9999", "Not Applicable" = "orange"),labels=c("Attaining     ",
                                                                                                                                                                    "Insufficient Information    ",
                                                                                                                                                                    "Non Attaining      "))

  if (sum(parameter_df$prop < .10) > 0) {
    pie_chart <- pie_chart + geom_text_repel(data = small_perc, aes(label = perc), size= 5, show.legend = F, nudge_x = nudgex,nudge_y = nudgey)
  }


  pie_chart
  }


#Indivdual pie charts to create combined pie charts
pie_do <- parameter_pie_chart('DO') 
pie_TP<-parameter_pie_chart('Total Phosphorus')
pie_temp<-parameter_pie_chart('Temperature')
pie_pH<-parameter_pie_chart('pH')
pie_arcs<-parameter_pie_chart('Arsenic-HH')
pie_TDS<-parameter_pie_chart('Total Dissolved Solids')
pie_causebio<-parameter_pie_chart('Biological (Cause Unknown)')
pie_human_lead<-parameter_pie_chart('Lead-HH - DWS')
pie_mercury<-parameter_pie_chart('Mercury-HH')
pie_nitrate<-parameter_pie_chart('Nitrate')
pie_aluminum <- parameter_pie_chart("Aluminum")
pie_temp_trout<-parameter_pie_chart('Temperature Trout')
pie_do_trout<-parameter_pie_chart('DO Trout')
pie_fish_merc<-parameter_pie_chart('Fish-Mercury')
pie_fish_ddt<-parameter_pie_chart('Fish-DDx')
pie_fish_dioxin<-parameter_pie_chart('Fish-Dioxin')
pie_fish_chlordane<-parameter_pie_chart('Fish-Chlordane')
pie_fish_pcb<-parameter_pie_chart('Fish-PCB')
pie_human_arsenic<-parameter_pie_chart('Arsenic-HH')
pie_TDS<-parameter_pie_chart('Total Dissolved Solids')
pie_arsenic_dws<-parameter_pie_chart('Arsenic HH - DWS')
pie_trout_do<-parameter_pie_chart('DO Trout')
pie_unknown_trout<-parameter_pie_chart('Biological Trout (Cause Unknown)')
pie_ecoli<-parameter_pie_chart('e.Coli')
pie_enterococcus<-parameter_pie_chart('Enterococcus')
pie_beach_enterococcus<-parameter_pie_chart('Beach Closing (Enterococcus)')
##Figure 2.10
combined_plot1 <- plot_grid(pie_human_arsenic + theme(legend.position="none"), 
                           pie_TDS + theme(legend.position = "none"),
                           pie_human_lead + theme(legend.position = "none"),
                           pie_mercury + theme(legend.position = "none"),
                           pie_nitrate + theme(legend.position = "bottom"),
                           nrow = 2,ncol=3,align="hv",labels=c("Arsenic,human health","TDS","Lead,human health","Mercury,human health","Nitrate"),label_fontface="bold",label_size=10,hjust=-0.3,vjust=9)+
                          draw_label("Figure 2.10:Assessment Results for Key Parameters Associated with Water Supply Use,\nPercent(%) of 826 AUs",fontface="bold",hjust=0.5,vjust=-14.5)
ggsave(filename="Figure2.10-Water Supply Use.pdf",path="V:/lum/WM&S/BEAR (Bureau of Environmental Analysis and Restoration)/Envpln/Hourly Employees/KevinZolea/Rwork/2014IR/PieCharts",width=11.5,height=11)
`

我有的情节: 在此处输入图像描述

我想要的剧情: 在此处输入图像描述

4

0 回答 0