用条形图和点图来排序标签
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用条形图和点图来排序标签相关的知识,希望对你有一定的参考价值。
我有一个使用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组的订购?
答案
每个组都没有相同的值集,那么您必须通过添加以下强制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)
另一答案
我同意@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
以上是关于用条形图和点图来排序标签的主要内容,如果未能解决你的问题,请参考以下文章
Fig4-a ggplot2绘制箱线图叠加散点图2020-12-14
R语言散点图可视化:自定义标题和标签拟合回归线lowess为散点图添加平滑拟合线修改散点图中点颜色和点符号分组散点图添加图例pairs可视化散点图矩阵ggplt2可视化lattice