'How do I adjust the width and resolution of a ggplot and the axis name?

I'm having difficulty adjusting the width and quality of my ggplot2 code here is the code:

# change in density:
density.diff.bleph <- data.density %>%
  group_by(species.involved, nutrient.level, prey, replicate, video.timing) %>%
  summarise(avg.bleph = mean(number.bleph)) %>% # taking average of all the blephs in a video here
  dplyr::select(species.involved, nutrient.level, prey, replicate, video.timing, avg.bleph) %>%
  pivot_wider(names_from = video.timing, values_from = avg.bleph) %>% # manipulate data frame to be able to take the difference
  mutate(density.change = Final - Initial) # calculate difference/change in density
ggplot(density.diff.bleph, aes(x=as.factor(species.involved), y=density.change, colour=as.factor(nutrient.level)))+geom_boxplot()+geom_hline(yintercept = 0)

I need a code that will allow me to adjust the resolution and width so it looks better when I copy and paste it out of there any suggestions? I've been told this code works:

print(plot_name)
    
    png(filename = "~/Desktop", width = 1500, height = 800, res=200) 
    
    plot(plot_name)
    
    dev.off()

but I honestly don't know how to incorporate it into my original code and I'm struggling also does anyone know know to change the bottom name of "as.factor(species.involved)" I'm really lost on that as well. here is the graph I'm trying to change the bottom name to Predator density instead of "as.factor (species.involved)"

enter image description here



Solution 1:[1]

Without your data it is difficult to give an answer that works, but you can try

library(tidyverse)

plot_data.density %>%
  group_by(species.involved, nutrient.level, prey, replicate, video.timing) %>%
  summarise(avg.bleph = mean(number.bleph)) %>% # taking average of all the blephs in a video here
  select(species.involved, nutrient.level, prey, replicate, video.timing, avg.bleph) %>%
  pivot_wider(names_from = video.timing, values_from = avg.bleph) %>% # manipulate data frame to be able to take the difference
  mutate(density.change = Final - Initial) %>% # calculate difference/change in density
  ggplot(aes(x = as.factor(species.involved), y = density.change, colour = as.factor(nutrient.level))) + 
    geom_boxplot() + 
    geom_hline(yintercept = 0) +
    labs(x = "Predator density", y = "Density change") + 
    theme(legend.title = element_blank())

To save the plot you can use

ggsave("your/path/file_name.png", plot_data.density, dpi = 300, width = 1500, height = 800)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leonardo