I created a barchart using ggplot2
, but some of my bars are merged together. This is the code I used:
library(ggplot2)
library(RColorBrewer)
bird_plot_error <- test %>%
group_by(bird) %>%
summarise(key = unique(dr),
dr = sum(dr)) %>%
group_by(bird) %>%
mutate(dr = dr/sum(dr) * key) %>%
ungroup %>%
mutate(bird = fct_reorder(bird, desc(bird))) %>%
ggplot(aes(x=bird, y=dr, fill="lightblue"))
geom_bar(stat="identity")
theme_classic()
theme(legend.position="none")
coord_flip()
scale_fill_brewer(palette="Paired")
ylim(0, 0.8)
bird_plot_error <- bird_plot_error ggtitle("Detection rate by bird")
xlab("Bird") ylab("Detection rate")
This is the output:
Why are two of my bars merged together in the center of the chart? Is there a way to fix this?
CodePudding user response:
Your graphics device is unable to display with enough resolution/size in your existing settings to properly indicate the spacing between all of those bars. Your options are:
Change the resolution option on your graphics output. If in R studio, you might just be able to see a difference for the "zoomed" version. If using
ggsave()
and saving a .png file, for example, you can set thedpi=
to a higher number.Save your plot to be "taller". If you are saving your plot with
ggsave()
, then set your width and height to stretch things out to show more spacing. In your plot, set the height taller and smaller and you should see a change.Set the width of your bars to make them skinnier. You can use the
width=
argument ofgeom_bar()
(which should begeom_col()
, BTW...). Smaller numbers will make the bars thinner, which shows more white space between them.Rethink your plot. If it's tough to fit all the data cleanly into one plot... think about whether or not the approach you are using is actually the best way to show your plot data. Are you expecting someone to use this plot to compare the difference between the 10th bar down and the 7th bar from the end? That's really tough to do with so many bars so... what's the overall thing you want someone who sees this plot to understand? Refocus and rethink your graphics to ensure they are showing the results of your experiments clearly. If you want to show all bars, maybe think about ways of grouping them... or set the
width=1
and force them all to "touch". There's a lot of options that depend on your goal... and maybe that would be a good question for another site here in stackexchange.
Good luck. Here is a reproducible example dataset with some pictures to show you how you can utilize 1 and 2 above:
# reproducible example dataset and plot
library(ggplot2)
set.seed(8675309)
df <- data.frame(
mythings=paste(rep(LETTERS, each = 2), 1:2, sep='-'),
myvalues=sample(1:100, 26*2, replace=TRUE))
ggplot(df, aes(x=mythings, y=myvalues))
geom_col(fill='steelblue')
coord_flip()
Here's what settings of dpi=100
looks like with width=5
, and height=5
for my png device. Your mileage may vary depending on your graphics settings:
ggsave('a_plot_100_dpi.png', dpi=100, width=5, height=5)
Note how you have some bars merging together? This is what happens when you stretch it to be taller (via height=10
):
ggsave('a_plot_100_dpi_taller.png', dpi=100, width=5, height=10)
The lines are gone, but it's really tall. To keep the same aspect ratio, you can try a higher resolution. For this example and my settings, dpi=300
seems to work:
ggsave('a_plot_300_dpi.png', dpi=300, width=5, height=5)