R-组内分组条形图排序
下面是一些R代码及其生成的图形:R-组内分组条形图排序,r,plot,graph,ggplot2,bar-chart,R,Plot,Graph,Ggplot2,Bar Chart,下面是一些R代码及其生成的图形: library(ggplot2) year <- c("1950", "1950", "1960", "1960", "1970", "1970") weight <- c(15, 10, 20, 25, 18, 20) name <- c("obj1", "obj2", "obj3", "obj4", "obj5", "obj1") object.data <- data.frame(year, weight, name) ggplot
library(ggplot2)
year <- c("1950", "1950", "1960", "1960", "1970", "1970")
weight <- c(15, 10, 20, 25, 18, 20)
name <- c("obj1", "obj2", "obj3", "obj4", "obj5", "obj1")
object.data <- data.frame(year, weight, name)
ggplot(object.data, aes(x=factor(year), y=weight,
fill=reorder(name, -weight))) + geom_bar(stat="identity", position="dodge")
库(ggplot2)
年
# Create a new variable with your desired order.
object.data1 = object.data %>%
group_by(year) %>%
mutate(position = rank(-weight))
# Then plot
ggplot(object.data1,
aes(x=year, y=weight, fill=reorder(name, -weight), group = position)) +
geom_bar(stat="identity", position="dodge")