0

I have created a ggplot2 graph that visualises the frequency of occurrence for different combinations of two factors. However, due to the large number of factors involved, it becomes challenging to interpret the plot effectively. To address this issue, I would like to add grid borders to the plot to improve its clarity and make it easier to understand.

The data used for the plot consists of two factors and a frequency value indicating how many times a specific combination occurred. Each factor represents a different category, and the frequency indicates the count of occurrences for each combination. How could I add grid border to make the data more readeable?

> str(data_df2)
'data.frame':   6111 obs. of  3 variables:
 $ Lure     : chr  "Acrolepiopsis assectella" "Adoxophyes orana" "Apamea oblonga" "Archips rosana" ...
 $ Species  : Factor w/ 97 levels "Zeiraphera isertana",..: 97 97 97 97 97 97 97 97 97 97 ...
 $ Frequency: int  0 0 0 0 0 0 0 0 0 0 ...

This is the code I used:

ggplot_obj2 <- ggplot(data = data_df2, aes(x = Lure, y = Species)) +
  geom_tile(aes(fill = Frequency)) +
  scale_fill_gradient(low = "gray90", high = "green4") +
  labs(x = "Lure", y = "Species", fill = "Frequency") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        axis.text.y = element_text(size = 6),
        legend.text = element_text(size = 8))

# Convert to plotly object
plotly_obj2 <- ggplotly(ggplot_obj2)    
plotly_obj2
reinoud
  • 63
  • 6
  • `geom_tile(... , colour = "white", linewidth = 1)` – margusl Jun 27 '23 at 09:55
  • I tried that, but gives me the same plot output without borders. – reinoud Jun 27 '23 at 10:28
  • 2
    Works with example datasets included in (`?geom_tile`)[https://ggplot2.tidyverse.org/reference/geom_tile.html#ref-examples]. I'm afraid your issue is not reproducible for others until some sample data is included in the question, i.e output of `dput(head(data_df2, n = enough_rows_to_reproduce_the_issue))` . You may need to reduce Lure and Species too to make the sample more compact. More details on reproducible examples on R tag info - https://stackoverflow.com/tags/r/info - and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – margusl Jun 27 '23 at 11:03
  • 2
    Are both ggplot and plotly affected? Title and tags suggest it's only about ggplot but included plotly code adds some ambiguity. – margusl Jun 27 '23 at 11:06
  • It works for ggplot but not for plotly indeed. I should have checked that first. Thanks! – reinoud Jun 30 '23 at 07:13
  • I *may* have accidentally reproduced it and it seems to be related to non-discrete fill. What happens if you use discrete fill scale, i.e. turn your fill variable to factor, `geom_tile(aes(fill = as.factor(Frequency)))` , and drop `scale_fill_gradient` for now ? Perhaps change a title, tags and description a bit to change and also include a portion from your dataset to set focus to `ggplotly()` and your input data. – margusl Jun 30 '23 at 08:03

0 Answers0