Radosław Gzyl
Radosław Gzyl

Reputation: 25

Inverse data in R

When I run the code, the values are not displayed like in an array, just the inverse. How to change it?

library(ggplot2)

DF <- data.frame(staw_łokciowy = c("zgięcie", "zgięcie", "wyprost", "wyprost"),
                 LEGENDA = c("przed", "po", "przed", "po"),
                 stopnie = c(4, 6, 8, 10))
ggplot(DF, aes(staw_łokciowy, stopnie, fill = LEGENDA)) +
  geom_col(position = 'dodge') +
  geom_errorbar(aes(ymin=stopnie-2, ymax=stopnie+2), 
                size=.5, width=.2,
                position = position_dodge(0.9))

Upvotes: 1

Views: 103

Answers (1)

Dmitry Zotikov
Dmitry Zotikov

Reputation: 2532

The sure-shot way is to reorder both the x-asis and the color variable using the y-axis variable, i.e.

ggplot(DF, aes(reorder(staw_łokciowy, stopnie), stopnie, fill = reorder(LEGENDA, stopnie))) +
  geom_col(position = 'dodge') +
  geom_errorbar(aes(ymin=stopnie-2, ymax=stopnie+2), 
                size=.5, width=.2,
                position = position_dodge(0.9))

enter image description here

Upvotes: 1

Related Questions