ggplot不会绘制缺少的类别 [英] ggplot will not plot missing category

查看:152
本文介绍了ggplot不会绘制缺少的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力与ggplot(我总是这样做)。有很多关于强制ggplot在传说中包含零值类别的非常类似的问题 -



当前绘图脚本

强:>

  #data 
plotData< - data.frame(TM= c(3,2 ,3,3,3,4,3,2,3,3,4,3,4,3,2,3,2,2,3,2,3,3,3,2,3,1,3 ,2,2,4,4,3,2,3,4,2,3),
得分= c(5,4,4,4,3,5,5,5,5, 5,5,3,5,5,4,4,5,4,5,4,5,4,5,4,4,4,4,4,5,4,4,5,3,5, 5,5,5))
#vars
xTitle < - bquote(T[M])
vI < - plotData $ TM
depVar< - plotData $ Score

#plot
p < - ggplot(plotData,aes_string(x = vI,y = depVar,color = vI))+
geom_point()+
geom_jitter(alpha = 0.8,position = position_jitter(width = 0.2,height = 0.2))+
geom_boxplot(width = 0.75,alpha = 0.5,aes_string(group = vI))+
theme_bw ()+
labs(x = xTitle)+
labs(y = NULL)+
theme(legend.position ='none',
axis.t ext = element_text(size = 10,face =bold),
axis.title = element_text(size = 16))

尝试的解决方案


  1. drop = False 缩放(建议由@Jarretinha 并手动设置标签 scale_fill_manual 不做任何事情,并产生与上述示例相同的右手绘图。

    > (Cat1),Cat2,Cat3, Cat4,Cat5),
    drop = FALSE)


  2. 使用此逻辑并尝试使用 scale_x_discrete 导致x轴上的类别名称发生更改,但第五个仍然丢失,并且边距(与尝试1一样)再次停止。但显然 scale_x_discrete 是重要的,而不是整个答案

    > p + scale_x_discrete(limits = c(Cat1,Cat2,Cat3,Cat4,Cat5)drop = FALSE $ b $ $ b



ANSWER

以上例子来自@Bouncyball& @aosmith


  #data 
plotData< - data.frame(TM= c(3,2, 3,3,3,4,3,2,3,3,4,3,4,3,2,3,2,2,3,2,3,3,3,2,3,1,3, 2,2,4,4,3,2,3,4,2,3),
分数= c(5,4,4,4,3,5,5,5,5,5 ,5,3,5,5,4,4,5,4,5,4,5,4,5,4,4,4,4,4,5,4,4,5,3,5,5 ,5,5))
plotData $ TM < - factor(plotData $ TM,levels = 1:5)#添加正确的(输入数据的期望数量的因子)

#vars
xTitle < - bquote(T[M])
vI < - plotData $ TM
depVar < - plotData $ Score
myPalette < - c ('#5c9bd4','#a5a5a4','#4770b6','#275f92','#646464','#002060')

#plot
ggplot(plotData,aes_string (x = vI,y = depVar,color = vI))+
geom_jitter(alpha = 0.8,position = position_jitter(width = 0.2,height = 0.2))+
geom_boxplot(width = 0.75,alpha = 0.5,aes_string(group = vI))+
scale_colour_manual(values = myPalette,drop = F)+在此处添加的新行
scale_x_discrete(drop = F)+#new line here b $ b theme_bw() +
labs(x = xTitle)+
labs(y = NULL)+
theme(legend.position ='none',
axis.text = element_text(size = 10 ,
axis.title = element_text(size = 16))

解决方案

以下是您可以使用的解决方法:

 #生成虚拟数据
set.seed(123)
df1< - data.frame(lets = sample字母[1:4],20,replace = T),
y = rnorm(20),stringsAsFactors = FALSE)
#define factor,包括丢失的类别作为级别
df1 $ let < - factor(df1 $ lets,levels = letters [1:5])
#make plot
ggplot(df1,aes(x = lets,y = y))+
geom_boxplot (aes(fill = lets))+
geom_point(data = NULL,aes(x ='e',y = 0),pch = NA)+
scale_fill_brewer (drop = F,palette ='Set1')+
theme_bw()



基本上,我们绘制了一个空白的点(即 pch = NA ),以便该类别在x轴上显示,但没有可见的 geom 与其关联。当<$ c c> 有五个级别时,我们还定义了我们的离散变量因子 $ C> data.frame 。缺少的类别是字母 e



注意:您必须调整这个空点的位置,使其不会偏斜y轴。



否则,您可以使用


I'm struggling with ggplot (I always do). There are a number of very similar questions about forcing ggplot to include zero value categories in legends - here and here (for example). BUT I (think I) have a slightly different requirement to which all my mucking about with scale_x_discrete and scale_fill_manual has not helped.

Requirement: As you can see; the right-hand plot has no data in the TM=5 category - so is missing. What I need is for that right plot to have category 5 shown on the axis but obviously with no points or box.

Current Plot Script:

#data
plotData <- data.frame("TM"    = c(3,2,3,3,3,4,3,2,3,3,4,3,4,3,2,3,2,2,3,2,3,3,3,2,3,1,3,2,2,4,4,3,2,3,4,2,3),
                       "Score" = c(5,4,4,4,3,5,5,5,5,5,5,3,5,5,4,4,5,4,5,4,5,4,5,4,4,4,4,4,5,4,4,5,3,5,5,5,5))
#vars
xTitle <- bquote("T"["M"])
v.I    <- plotData$TM
depVar <- plotData$Score

#plot
p <- ggplot(plotData, aes_string(x=v.I,y=depVar,color=v.I)) +
  geom_point() +
  geom_jitter(alpha=0.8, position = position_jitter(width = 0.2, height = 0.2)) +
  geom_boxplot(width=0.75,alpha=0.5,aes_string(group=v.I)) +
  theme_bw() +
  labs(x=xTitle) +
  labs(y=NULL) +
  theme(legend.position='none', 
        axis.text=element_text(size=10, face="bold"),
        axis.title=element_text(size=16))

Attempted Solutions:

  1. drop=False to scales (suggested by @Jarretinha here) totally borks margins and x-axis labels

    > plot + scale_x_discrete(drop=FALSE) + scale_fill_manual(drop=FALSE)

  1. Following logic from here and manually setting the labels in scale_fill_manual does nothing and results in the same right-hand plot from example above.

    > p + scale_fill_manual(values = c("red", "blue", "green", "purple", "pink"), labels = c("Cat1", "Cat2", "Cat3", "Cat4", "Cat5"), drop=FALSE)

  2. Playing with this logic and trying something with scale_x_discrete results in a change to category names on x-axis but the fifth is still missing AND the margins (as attempt 1) are borked again. BUT apparent that scale_x_discrete is important and NOT the whole answer

    > p + scale_x_discrete(limits = c("Cat1", "Cat2", "Cat3", "Cat4", "Cat5"), drop=FALSE)

ANSWER for above example courtesy of input from @Bouncyball & @aosmith

#data
plotData    <- data.frame("TM"    = c(3,2,3,3,3,4,3,2,3,3,4,3,4,3,2,3,2,2,3,2,3,3,3,2,3,1,3,2,2,4,4,3,2,3,4,2,3),
                       "Score" = c(5,4,4,4,3,5,5,5,5,5,5,3,5,5,4,4,5,4,5,4,5,4,5,4,4,4,4,4,5,4,4,5,3,5,5,5,5))
plotData$TM <- factor(plotData$TM, levels=1:5) # add correct (desired number of factors to input data)

#vars
xTitle <- bquote("T"["M"])
v.I    <- plotData$TM
depVar <- plotData$Score
myPalette <- c('#5c9bd4','#a5a5a4','#4770b6','#275f92','#646464','#002060')

#plot
ggplot(plotData, aes_string(x=v.I,y=depVar,color=v.I)) +
  geom_jitter(alpha=0.8, position = position_jitter(width = 0.2, height = 0.2)) +
  geom_boxplot(width=0.75,alpha=0.5,aes_string(group=v.I)) +
  scale_colour_manual(values = myPalette, drop=F) +  # new line added here
  scale_x_discrete(drop=F) + # new line added here
  theme_bw() +
  labs(x=xTitle) +
  labs(y=NULL) +
  theme(legend.position='none', 
        axis.text=element_text(size=10, face="bold"),
        axis.title=element_text(size=16))

解决方案

Here's a workaround you could use:

# generate dummy data 
set.seed(123)
df1 <- data.frame(lets = sample(letters[1:4], 20, replace = T),
                  y = rnorm(20), stringsAsFactors = FALSE)
# define factor, including the missing category as a level
df1$lets <- factor(df1$lets, levels = letters[1:5])
# make plot
ggplot(df1, aes(x = lets, y = y))+
    geom_boxplot(aes(fill = lets))+
    geom_point(data = NULL, aes(x = 'e', y = 0), pch = NA)+
    scale_fill_brewer(drop = F, palette = 'Set1')+
    theme_bw()

Basically, we plot an "empty" point (i.e. pch = NA) so that the category shows up on the x-axis, but has no visible geom associated with it. We also define our discrete variable, lets as a factor with five levels when only four are present in the data.frame. The missing category is the letter e.

NB: You'll have to adjust the positioning of this "empty" point so that it doesn't skew your y axis.

Otherwise, you could use the result from this answer to avoid having to plot an "empty" point.

# generate dummy data 
set.seed(123)
df1 <- data.frame(lets = sample(letters[1:4], 20, replace = T),
                  y = rnorm(20), stringsAsFactors = FALSE)
# define factor, including the missing category as a level
df1$lets <- factor(df1$lets, levels = letters[1:5])
# make plot
ggplot(df1, aes(x = lets, y = y)) +
    geom_boxplot(aes(fill = lets)) +
    scale_x_discrete(drop = F) +
    scale_fill_brewer(drop = F, palette = 'Set1') +
    theme_bw()

这篇关于ggplot不会绘制缺少的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆