订购带有条形图和点图的标签

时间:2018-02-28 18:06:57

标签: r ggplot2

我有一个使用geom_bar()的条形图,我想使用geom_point()覆盖点。问题是轴标签的排序。我有2个组,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组的最后一个栏中,该栏位于其所属的任何超类别中。

但是当我尝试添加点时,排序变得混乱。运行此代码,只需将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组的排序?

2 个答案:

答案 0 :(得分:0)

每个组都没有相同的值集,那么您必须通过添加:

来强制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)

答案 1 :(得分:0)

我同意@CédricMiachon。

使用不同的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

enter image description here