Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205491

Order Columns in ggplot

$
0
0

I've tried what others have suggested (Order bars in ggplot and Order Bars in ggplot2 bar graph) for ordering columns and I still can't get it.

I have a dataset with a column called "Zap", which has values that I want sorted from the highest occurring count to the least. Here is my R code:

mydata %>% 
  group_by(Zap) %>% 
  summarize(count = n()) %>% 
  mutate(percent = count/sum(count)) %>% 
  ggplot(aes(x=Zap, y=count, fct_infreq(Zap))) +
  xlab("EA5 Zigbee Enabled")+
  geom_col() +
  geom_text(aes(label = paste0(round(100 * percent, 1), "%")), vjust = -0.25)

That code didn't actually reorder anything. This is what I'm getting (not sorted)

enter image description here

I also tried this:

mydata %>% 
  group_by(Zap) %>% 
  summarize(count = n()) %>% 
  mutate(percent = count/sum(count)) %>% 
  ggplot(aes(x=Zap, y=count)) +

  x=reorder(Zap,Zap, function(x)-count(x)) +

  xlab("EA5 Zigbee Enabled")+
  geom_col() +
  geom_text(aes(label = paste0(round(100 * percent, 1), "%")), vjust = -0.25)

But this code gives me an error:

Error in reorder(Zap, Zap, function(x) -count(x)) : object 'Zap' not found

Any ideas what I'm doing wrong?


Viewing all articles
Browse latest Browse all 205491

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>