0

I have made a stacked bar chart, but would like to arrange my x-axis in a way where it is ascending in frequency of my first variable. My data is all in strings, and I have 4 different strings (string A, string B, string C, and string D). I have been trying to get it so that my stacked bar chart is sorted by the frequency of string A, but do not know how.

I have been trying to use factor() and order() and name[order()] but they are all causing a lot of errors.

data$variable <- factor(data$variable, levels = data$variable[order(data$val)])

ggplot(data = data, aes(x = variable, y = countofdata, fill = factor(value))) 
+ geom_bar(
stat = "identity")
bensir
  • 35
  • 4

1 Answers1

0

Something like this could do the trick

# generate data
data <- data.frame(variable= factor(c("a", "a", "a", "c", "d", "d")))
# see the order of the levels
data$variable
# [1] a a a c d d
# Levels: a c d

# change factor levels ascending
data$variable <- factor(data$variable, levels = names(sort(table(data$variable))))
# output
data$variable
# [1] a a a c d d
# Levels: c d a
# -> correct order

I can not test whether this will workout for you because you did not provide any data. Please do so next time (see here).

EDIT

Edit based on comments

categories <- c("category 3", "category 3", "category 3", "category 2", "category 2", "category 2", "category 1", "category 1", "category 1")
values <- c("A", "B", "C", "A", "A", "A", "A", "A", "C")
data <- cbind.data.frame(categories, values)

tabl <- aggregate(values ~ categories, data, table)
data$categories <- factor(data$categories, levels = as.character(tabl$categories[order(tabl[ , "values"][ , "A"], decreasing= FALSE)]))
data$categories
# Levels: category 3 category 1 category 2
  • This is very helpful! Thank you! I have one question though. I currently have two categories, where one contains all of my category names (category 1, category 2, etc) and the other is a string assignment for each category name. i.e. I may have data with (category 1, A), (category 1, A), (category 1, B), (category 3, B), etc. Every category has the same number of inputs. I am trying to make it so I sort category 1, category 2, and category 3 by amount of times they have string A. Apologies for being terrible at phrasing the question – bensir Jul 30 '19 at 08:49
  • @bensir It would be much easier to understand if you provide the data. I don't understand it this way. Anyway, this sounds like a new question to me or is it still about factor order? If it is a new question, you can ask it if it does not work out. –  Jul 30 '19 at 08:53
  • I will put together a quick data set one second. Here is a quick subset of what it may look like: categories <- c("category 3", "category 3", "category 3", "category 2", "category 2", "category 2", "category 1", "category 1", "category 1") values <- c("A", "B", "C", "A", "A", "A", "A", "A", "C") data <- cbind(categories, values) – bensir Jul 30 '19 at 08:54
  • @bensir You can do so but keep in mind that on StackOverflow you are supposed to ask one question at a time. Is the new question also about factor order? –  Jul 30 '19 at 08:56
  • Yes this is still about factor order. this data has category 1 with 1 A assignment, category 2 has 3 A's, and category 3 has 2 A's so I am hoping to order it all in order of category 2, category 3, then category 1 (descending based on frequency of A's) – bensir Jul 30 '19 at 09:00
  • @bensir first, why descending? Your question says ascending. Second, only on As? So the other letters do not matter? –  Jul 30 '19 at 09:04
  • Yes. Ascending or descending I can fix easily, the main point is figuring out how I can sort based purely on A's. The difficulty is that I do not know how to order the categories section of my data based on the frequency of only A's per category, while ignoring B's and C's if that makes sense? – bensir Jul 30 '19 at 09:06
  • thank you so much! I have spent hours trying to tackle this problem! If there is a way to make this post less of a mess for others in the future please let me know how I can clean it up. Thank you so much again, and apologies one last time for my lack of understanding of how to phrase my question properly. – bensir Jul 30 '19 at 09:28
  • @bensir You can try completely rephrase your question which is more about factor levels than about ggplot. Further, I am happy that it worked out for you! Consider upvoting my answer and mark it as answered because this ia how this website works. –  Jul 30 '19 at 09:32