0

I'm looking for a way to re-order the bar plot produced with ggplot2 such that the rates of the less observed category (i.e. ThemeFirst) increase from the left to the right. The original bar plot I generated is below:

enter image description here

And it is plotted using the following codes:`

t1<-table(data$Variety,data$realization)
dataframe_realization<-data.frame(Variety=names(prop.table(t1,1)[,1]),
                                  RecipientFirst=prop.table(t1,1)[,1],
                                  ThemeFirst=prop.table(t1,1)[,2],
                                  row.names=NULL)

dataframe_realization<-melt(dataframe_realization,id="Variety",variable_name="Variant")

# adding absolute frequency values to the table
dataframe_realization_absfreq<-data.frame(Variety=names(t1[,1]),
                                          RecipientFirst=as.numeric(t1[,1]),
                                          ThemeFirst=as.numeric(t1[,2]))
dataframe_realization_absfreq<-melt(dataframe_realization_absfreq,id="Variety",variable_name = "Variant")
dataframe_realization$absvals<-dataframe_realization_absfreq$value
dataframe_realization$Proportion<-dataframe_realization$value
dataframe_realization$variable<-dataframe_realization$Variant

labels.order <- dataframe_realization %>% 
  filter(Variety == '14th-18thCentury') %>% 
  arrange(Proportion) %>% 
  pull(Variant)

df.new <- dataframe_realization %>% 
  mutate(
    Variable = factor(Variant, levels = labels.order, ordered = T)
  )

# stacked bar plot with absolute values added on the each bar
realization_plot_absvals<-ggplot(data = dataframe_realization, aes(Variety, Proportion, group = Variant)) +
  geom_col(aes(fill = Variant)) +
  labs(title = "", y="Proportion of theme-recipient tokens", x="") + 
  scale_y_continuous() +
  scale_fill_grey(start = 0.25, end = 0.75) +
  geom_text(aes(label = absvals), position = position_stack(vjust = 0.5),color=ifelse(dataframe_realization$Variant=="RecipientFirst", "white", "black"), ) +
theme(text=element_text(size=15))
ggsave("~/VADIS_VarietyProportion_absvals.png",realization_plot_absvals, width=13, height=6, units="in", dpi = 1000)

So, again, the idea is to rearrange the plot and the bar to the far left will be the "Variety" with least ThemeFirst proportion (namely 19thCentury), and the bar to the far right will be the one with most ThemeFirst proportion (namely CTM_Other). The data for producing the plot can be found in this OSF page.

1 Answers1

0

Just make a factor of the x-axis variable, with the levels based on the order of proportions like you did for labels.order.

library(dplyr)
library(reshape2)
library(ggplot2)

variety.order <- dataframe_realization %>% 
  filter(Variant == labels.order[1]) %>% 
  arrange(Proportion) %>% 
  pull(Variety)

df.new <- dataframe_realization %>% 
  mutate(
    Variable = factor(Variant, levels = labels.order, ordered = T),
    Variety = factor(Variety, levels = variety.order)
  )

# stacked bar plot with absolute values added on the each bar
realization_plot_absvals<-ggplot(data = df.new, aes(Variety, Proportion, group = Variant)) +
  geom_col(aes(fill = Variant)) +
  labs(title = "", y="Proportion of theme-recipient tokens", x="") + 
  scale_y_continuous() +
  scale_fill_grey(start = 0.25, end = 0.75) +
  geom_text(aes(label = absvals), position = position_stack(vjust = 0.5),color=ifelse(dataframe_realization$Variant=="RecipientFirst", "white", "black"), ) +
  theme(text=element_text(size=15))

figure with ordered x-axis

(note: the option variable_name in the function reshape::melt should be variable.name)

Leon Samson
  • 405
  • 3
  • 12