0

if I have the following plot, how can I prohibit reshuffling of data before plotting? I would like to maintain the same order of data in the plot as in the data frame which I am trying to plot. Any help is much appreciated

df <- data.frame(derma=c(1:14), prevalence=c(1:14))

df$derma = c("Spotted melanosis","Diffuse melanosis on palm","Spotted melanosis on trunk","Diffuse melanosis on trunk","Leuco melanosis","Whole body melanosis","Spotted keratosis on palm","Diffuse keratosis on palm","Spotted keratosis on sole","Diffuse keratosis on sole","Dorsal keratosis","Chronic bronchitis","Liver enlargement","Carcinoma")
df$prevalence = c(16.2,78.6,57.3,20.6,17,8.4,35.4,23.5,66,52.8,39,6,2.4,1)

g <- ggplot(data=df, aes(x=derma, y=prevalence)) + geom_bar(stat="identity") + coord_flip()

print(g)

enter image description here

Stücke
  • 868
  • 3
  • 14
  • 41

1 Answers1

0

As mentioned in the comments, you can do this by ordering the factor variable. Below is the sample code:

df <- data.frame(derma=c(1:14), prevalence=c(1:14))

df$derma = c("Spotted melanosis","Diffuse melanosis on palm","Spotted melanosis on trunk","Diffuse melanosis on trunk","Leuco melanosis","Whole body melanosis","Spotted keratosis on palm","Diffuse keratosis on palm","Spotted keratosis on sole","Diffuse keratosis on sole","Dorsal keratosis","Chronic bronchitis","Liver enlargement","Carcinoma")
df$prevalence = c(16.2,78.6,57.3,20.6,17,8.4,35.4,23.5,66,52.8,39,6,2.4,1)
df$derma <- factor(df$derma, levels = df$derma, ordered=TRUE)

g <- ggplot(data=df, aes(x=derma, y=prevalence)) + geom_bar(stat="identity") + coord_flip()

print(g)

You can play around with the labels and give it the order that you want in your plot. For example, you can reverse the elements in the plot by simply using the rev() function. Below is the code for same :

df <- data.frame(derma=c(1:14), prevalence=c(1:14))

df$derma = c("Spotted melanosis","Diffuse melanosis on palm","Spotted melanosis on trunk","Diffuse melanosis on trunk","Leuco melanosis","Whole body melanosis","Spotted keratosis on palm","Diffuse keratosis on palm","Spotted keratosis on sole","Diffuse keratosis on sole","Dorsal keratosis","Chronic bronchitis","Liver enlargement","Carcinoma")
df$prevalence = c(16.2,78.6,57.3,20.6,17,8.4,35.4,23.5,66,52.8,39,6,2.4,1)
df$derma <- factor(df$derma, levels = rev(df$derma), ordered=TRUE)

g <- ggplot(data=df, aes(x=derma, y=prevalence)) + geom_bar(stat="identity") + coord_flip()

print(g)
Kumar Manglam
  • 2,780
  • 1
  • 19
  • 28
  • Hi @Kumar, many thanks for your response! But actually, ordering is exactly what I **don't** want. I just want the data to appear in the same order as in the data.frame. Also if I use the order function only the labels are changed but not the data in the bars. So what happens at the moment is that the order of the df$derma is correct but somehow the data of the df$prevalence is changed in order but I would like it to remain in the same order. – Stücke Jul 25 '16 at 16:28
  • Sorry, I edited the code a bit. I used labels argument instead of levels. Now the data will not change order. I dont know the solution without ordering the factor. When you make a factor variable ordered, you are not actually ordering the data, but just specifying the how each level is ordered. Hence the output shows ordered plot, in the order that factor is. – Kumar Manglam Jul 26 '16 at 10:48
  • That's exactly what I've been looking for! Thank you very much for your contribution :) – Stücke Jul 26 '16 at 17:41