0

I am trying to make a heatmap using R ggplot, and the desired outcome graph is like below. I have different age ranges by each study, but I would like to fix the age range from 0 to 100 by 10 years old like this graph. I would like to populate the prevalence tiles by the defined age ranges in the raw data I have, but currently I am unable to populate the prevalence rates by the age ranges that are in the raw data. enter image description here

my data frame looks like this: enter image description here

This graph is the one that I made right now, with the x axis age range enter image description here

ggplot(CountryModel, aes(x = age, y = country, fill = prevalence)) +
  geom_tile(width = 0.9, height = 0.1) +  # adjust width and height of tiles
  facet_wrap(~ region, ncol = 1, scales = "free_y") +
  scale_fill_gradient(low = "white", high = "red", limits = c(0, 100), na.value = "gray") +  # fill the color for missing values
  labs(x = "Age Group", y = "Subgroup", fill = "Value") +
  theme_dark() +
  theme(panel.spacing.y = unit(0.1, "lines"),  # adjust spacing between panels
        axis.line = element_blank(),  # remove axis lines
        axis.text.x = element_text(angle = 45, hjust = 1),  # rotate x-axis labels
        axis.ticks = element_blank(),  # remove axis ticks
        strip.background = element_rect(fill = "gray10"),  # change background color of strip
        strip.text = element_text(color = "white"),  # change color of strip text
        panel.grid = element_blank(),  # remove panel grid
        legend.position = "bottom")+  # move legend to the bottom
  geom_vline(xintercept = seq(0,100,10), color = "grey")

the above is the code that I made.

I would appreciate your help!!

  • For the kind of chart you are trying to achieve I would probably go for `geom_segment` instead of `geom_tile`. To this end I would split the categorical age ranges in two columns containing the start and end age for each range. For more help please provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Mar 08 '23 at 08:16
  • thank you I edited and uploaded the dataframe I am working on! – Hyolim Kang Mar 08 '23 at 08:28
  • Hi Hyolim. Images of data aren't that useful. To share your data type `dput(CountryModel[1:20, ])` in the console and copy the output into your post. This makes it easy for others to copy your data and run your code. For more info see the link I posted in my first comment. – stefan Mar 08 '23 at 08:37

0 Answers0