Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change fill colors orientation

Is there a way to change the orientation of the filling colors from outside to inside (center) in this type of plot?

Imgur

Here is my code:

dat <- structure(list(
    Category = structure(1:15,
      .Label = c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7", "ID8", "ID9", "ID10", 
         "ID11", "ID12", "ID13", "ID14", "ID15"), class = "factor"),
    Percentage = c(0.8, 0.32, 0.15, 0.6, 0.4, 0.5, 0.3, 0.7, 1, 0.8, 0.3, 0.9, 0, 0.2, 0.46)),
  .Names = c("Category", "Percentage"), row.names = c(NA, 15L), class = "data.frame")

ggplot(data = dat) +
  geom_col(aes(x = Category, fill = Category, y = Percentage), width = 1) +
  geom_hline(yintercept = 1, color = "darkgrey", size = 1) + #ponto de partida
  geom_segment(aes(x = 1.5, xend = 4.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 5.5, xend = 15.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 1.5, xend = 15.5, y = .5, yend = .5), color = "darkgrey", size = 1) +#2018
  geom_segment(aes(x = .5, xend = 4.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_segment(aes(x = 5.5, xend = 15.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_vline(xintercept = seq(.5, 15.5, by = 1), color = "darkgrey", size = 1) +
  coord_polar() +
  theme_minimal() +
  labs(x = NULL, y = NULL) +
  theme(axis.text.y = element_blank(),
        legend.position = "none",
        panel.grid = element_blank())
like image 772
Rafael Cunha Avatar asked Jan 31 '26 04:01

Rafael Cunha


1 Answers

You just need to add scale_y_reverse() to have the y values start on the outside.enter image description here

Nice looking chart by the way, and thanks for the reproducible example!

ggplot(data = dat) +
  geom_col(aes(x = Category, fill = Category, y = Percentage), width = 1) +
  geom_hline(yintercept = 1, color = "darkgrey", size = 1) + #ponto de partida
  geom_segment(aes(x = 1.5, xend = 4.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 5.5, xend = 15.5, y = .75, yend = .75), color = "darkgrey", size = 1) +#2017
  geom_segment(aes(x = 1.5, xend = 15.5, y = .5, yend = .5), color = "darkgrey", size = 1) +#2018
  geom_segment(aes(x = .5, xend = 4.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_segment(aes(x = 5.5, xend = 15.5, y = .25, yend = .25), color = "darkgrey", size = 1) +#2019
  geom_vline(xintercept = seq(.5, 15.5, by = 1), color = "darkgrey", size = 1) +
  scale_y_reverse()+
  coord_polar() +
  theme_minimal() +
  labs(x = NULL, y = NULL) +
  theme(axis.text.y = element_blank(),
        legend.position = "none",
        panel.grid = element_blank())

Also, to render in high resolution:

require(Cairo)
ggsave("/path/", height=10,width=10, dpi=300, type="cairo-png")
# change height/width/dpi as necessary
like image 80
Mako212 Avatar answered Feb 02 '26 20:02

Mako212



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!