0

This is my code:

library(readxl)
ttable <- read_excel("/Users/jhan/Downloads/test.xlsx")

library(ggplot2)  # Data visualization
library(dplyr)   # Data manipulation
library(gridExtra)

data.test <- ttable[,c('patient', 'zpati')]
data.test <- na.omit(data.test)
data.test <- data.test %>% group_by(patient) %>% summarise(zpati = sum(zpati), .groups = 'drop') %>% mutate(Procent = paste0(round((zpati/sum(zpati)*100),1), '%'))
data.test <- arrange(data.test,Procent)

pie3 = ggplot(data.test, aes(x="", y=Procent, fill=patient)) + geom_bar(stat="identity", width=1)+ 
coord_polar("y", start=0) + 
geom_text(aes(label =Procent),size = 3, position = position_stack(vjust = 0.5))+
  
scale_fill_manual(values=c("#339ed1", "#c1d333", "#eaae33", "#87888a"), limits=c("GKV","Privat","BG/Unfall","Krankhaus")) +
 

labs(x = NULL, y = NULL, fill = NULL, title = "Patientenstruktur")+ theme_classic() + 
theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          legend.direction = "vertical",
          legend.position = "bottom",
          legend.key.size = unit(0.25, "cm"),
          legend.key.width = unit(0.25,"cm"),
          legend.text = element_text(size = 8),
                                     
          plot.title = element_text(hjust = 0.5, color = "#339ed1")) 
pie3

This is my code, and I want to reorder my y values like the order of fill("GKV","Privat","BG/Unfall","Krankhaus") my Y-variable ist not stored as a factor this ist numbers. and i export the data and make many number of Graphs. so the number would be changes according to the data.

enter image description here

한정근
  • 1
  • 1
  • 1
    Does this answer your question? [Reorder levels of a factor without changing order of values](https://stackoverflow.com/questions/2375587/reorder-levels-of-a-factor-without-changing-order-of-values) – ek-g Jul 13 '20 at 12:51

1 Answers1

0

If you ensure that your y-variable is stored as a factor, then you can set the levels (order) of these factors. It would look something like this:

pie3 = ggplot(data.test, aes(x="", y=factor(Procent,levels = c("GKV","Privat","BG/Unfall","Krankhaus"), fill=patient))
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • my Y-variable ist not stored as a factor this ist numbers. and i export the data and make many number of Graphs. so the number would be changes according to the data – 한정근 Jul 14 '20 at 13:13