我有一个使用geom_bar()的条形图,我想用geom_point()覆盖这些点。问题是轴标签的顺序。我有两个组,A组,我想用geom_bar()从高到低排序,B组,我想用geom_bar表示点。A组和B组不会总是有相同的类别,但我总是希望A组显示条形图,并从高到低排序。和
如果运行此代码,您将看到正确排序的条形图。我需要首先显示宠物超级类别,然后显示汽车类别。我已经将超范畴定义为有序因子,并且它是有效的。
然后在超类别中,根据gorup A的值从高到低对条进行排序。你可以看到,在宠物类别中,狗比其他类别的高,起亚在汽车类别中比其他类别高。
library(dplyr)
group = c("A","A","A","B","B","B","A","A","A","B","B","B")
supercategory = c("pet", "pet","pet","pet","pet","pet","car","car","car","car","car","car")
category = c("bird","cat","dog","bird","cat","lizard","ford","chevy","kia","kia","toyota","ford")
supercategory = factor(supercategory, levels= c("pet", "car"), ordered = TRUE)
value=c(3,4,5,4,5,6,1,3,10,8,3,5)
dat = data.frame(group = group,supercategory = supercategory, category = category, value = value )
dat = dat %>% mutate(LABEL = paste0(supercategory, "-",category), HIGH_VALUE = ifelse(group =="A",value,0)) %>%
arrange(supercategory, -HIGH_VALUE)
# after the lines above the data is ordered correctly. first by supercategory then by group A's value from higest to lowest using the HIGH_VALUE field
dat$ROW_NUMBER = 1:nrow(dat)
dat = dat %>% group_by(supercategory,category) %>% mutate(ROW_NUMBER2= min(ROW_NUMBER)) %>% arrange( supercategory ,ROW_NUMBER2)
# after the 2 lines above now the data is sorted by ROW_NUMBER2 which orders the category within supercategory.
# Group A will be be in bars using geom_bar
# group B will be displayed iwht points using geom_point
# The bars and points should be in the order of ROW_NUMBER2
library(ggplot2)
dat$LABEL = factor(dat$LABEL, levels = unique(dat$LABEL), ordered = TRUE)
ggplot(dat[dat$group=="A",] , aes(x = LABEL, y = value))+
geom_bar(stat="identity") 我想保持上面图的顺序,只添加条上面的点。如果B组有一个不属于A组的类别,那么无论A组属于哪个超级类别,点都应该在A组最后一个栏的右边。
但是当我尝试添加这些点时,排序就变得混乱了。运行这段代码,它只是将组B的数据添加为点,您将看到标签的顺序被打乱。
library(ggplot2)
dat$LABEL = factor(dat$LABEL, levels = unique(dat$LABEL), ordered = TRUE)
ggplot(dat[dat$group=="A",] , aes(x = LABEL, y = value))+
geom_bar(stat="identity") +
geom_point(data = dat[dat$group=="B",], aes(x = LABEL, y = value), shape=15, size = 3, color = "blue" )如何将此行添加到绘图中:
geom_point(data = dat[dat$group=="B",], aes(x = LABEL, y = value), shape=15, size = 3, color = "blue" )同时保持A组的排序?
发布于 2018-03-01 03:19:39
每组具有不同的值集,则必须通过添加以下内容来强制X轴顺序:
+ scale_x_discrete(limits=dat$LABEL)然后:
ggplot(data = dat , aes(x = LABEL, y = value) ) +
geom_bar(data = dat[dat$group=="A",], stat="identity") +
geom_point(data = dat[dat$group=="B",], shape=15, size = 3, color = "blue") +
scale_x_discrete(limits=dat$LABEL)发布于 2018-03-01 03:23:25
我同意@Cédric Miachon。
使用不同的x存在一个问题。改变行为的一种可能的方法是将NA引入不存在的x:
require(reshape2)
require(dplyr)
require(tidyr)
vector_f <- unique(dat$LABEL)
dat1 <- dat %>%
dcast(group+supercategory~LABEL, value.var = 'value') %>% #casting and gathering
gather(label, value , 3:10)
ggplot() +
geom_bar(data = dat1[dat1$group=="A",],aes(x = factor(label, levels = vector_f), y = value), stat="identity") +
geom_point(data = dat1[dat1$group=="B",], aes(x = factor(label, levels = vector_f), y = value))
##I removed some of the geom_point layout specs

https://stackoverflow.com/questions/49035934
复制相似问题