File:Geom bar reorder.png

From 太極
Jump to navigation Jump to search

Geom_bar_reorder.png(559 × 414 pixels, file size: 15 KB, MIME type: image/png)

Summary

library(ggplot2)
library(forcats)

data <- data.frame(
  category = c("A", "B", "C", "D"),
  value = c(3, 5, 2, 8)
)

data$category <- fct_reorder(data$category, data$value)

levels(data$category)
# [1] "C" "A" "B" "D"

ggplot(data, aes(x = category, y = value, fill = category)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  labs(x = "Category", y = "Value") +
  theme_minimal()

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current15:54, 13 August 2024Thumbnail for version as of 15:54, 13 August 2024559 × 414 (15 KB)Brb (talk | contribs)<syntaxhighlight lang='r'> library(ggplot2) library(forcats) data <- data.frame( category = c("A", "B", "C", "D"), value = c(3, 5, 2, 8) ) data$category <- fct_reorder(data$category, data$value) levels(data$category) # [1] "C" "A" "B" "D" ggplot(data, aes(x = category, y = value, fill = category)) + geom_bar(stat = "identity") + coord_flip() + labs(x = "Category", y = "Value") + theme_minimal() </syntaxhighlight>

The following page uses this file: