Search

# Bar charts in polar coordinates using R package ggplot2

R code providing both an ordinary bar plot and a plot based on polar coordinates.

library(RColorBrewer) library(ggplot2) Geography = c(rep(‘Ukraine in general’,3),rep(‘South-Eastern Ukraine’,3),rep(‘Lower-Southern Ukraine’,3),rep(‘Central Ukraine/Kiev’,3),rep(‘Western Ukraine’,3)) Answer = rep(c(“Ukraine and Russia must unite into a single state”, “Ukraine and Russia must be independent, but friendly states – with open borders, without visas and customs houses”,”Relations should be the same as with other states – with closed borders, visas and customs houses”)) numbers = c(12.5,68,14.7,25.8,72.2,2,19.4,63.8,10.5,5.4,69.7,20.9,0.7,66.7,24) df = data.frame(Geography,Answer,numbers) #install.packages(“extrafont”) library(extrafont) loadfonts(device=”win”) ggplot(df, aes(x = Geography)) + geom_bar(aes(weight=numbers, fill = Answer), position = ‘fill’) + scale_y_continuous(“”, breaks=NULL) + scale_fill_manual(values = rev(brewer.pal(6, “Oranges”))) + ggtitle(“What would you like to the relationship between Ukraine and Russia look like? \n Source: KIIS and DIF feb. 2014, n=2023 (NA 4.7 pct)”)+theme(legend.position=”bottom”,legend.direction=”vertical”,plot.title=element_text(family=””, face=”bold”, size=14),text=element_text(family=”Times”, face=”bold”, size=10),legend.title=element_blank())

#plot the stacked bar plot with polar coordinate ggplot(df, aes(x = Geography)) + geom_bar(aes(weight=numbers, fill = Answer), position = ‘fill’) + scale_y_continuous(“”, breaks=NULL) + scale_fill_manual(values = rev(brewer.pal(6, “Blues”))) + ggtitle(“What would you like to the relationship between Ukraine and Russia look like? \n Source: KIIS and DIF feb. 2014, n=2023 (NA 4.7 pct)”)+coord_polar()+theme(legend.position=”bottom”,legend.direction=”vertical”,plot.title=element_text(family=””, face=”bold”, size=14),text=element_text(family=”Times”, face=”bold”, size=10),legend.title=element_blank())

0 views0 comments

### Recent Posts

See All

#### SAS University Edition load error solution

Using SAS University Edition with VirtualBox in Fedora requires kernel signing of 3 modules. Avoid this by disabling Secure Boot in BIOS settings. Secure Boot makes it impossible to load nonauthorized

©2020 by Danish Institute for Data Science. Proudly created with Wix.com