在小平面图中省略x轴级别而不显示数据,并更改小节的宽度 [英] Omit x axis levels with no data in a facetted plot and change widths of the bars

查看:203
本文介绍了在小平面图中省略x轴级别而不显示数据,并更改小节的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如您在下面的数据中看到的,对于某些分面变量项目缺少x轴变量type的某些级别。例如,items = 32没有type = A。



我想沿着空白空间 x轴对应于不存在的类型(例如,对于32个物品的情况,类型A)。

一些数据(temp):

 类型项目值
A 16 6.3
B 16 8.3
C 16 7.9
B 32 7.7
C 32 8.3
C 64 7.9

绘图代码:

  library(ggplot2)
ggplot(temp,aes(x = type,y = value,fill = type))+
geom_bar(stat =identity)+
facet_grid(。〜items)



====================== =



编辑:

根据t o Joran的解决方案,设置 scales =free_x正在做我想做的事情。但是,在条目号32和64下,条的宽度变得非常大。请帮助我将所有条的宽度均匀化。

  ggplot(temp,aes(x = type,y = value,fill = type))+ 
geom_bar(stat =identity)+
facet_grid(。items,scales = free_x)

解决方案

只需遵循


As you can see in the data below, some levels of the x axis variable "type" are missing for some of the facetting variables "items". For example, there is no "type = A" for "items = 32".

I want to get rid of the empty space along the x axis corresponding to non-existing "types" (e.g., type A for 32 items case).

Some data ("temp"):

 type   items     value
    A      16       6.3
    B      16       8.3
    C      16       7.9
    B      32       7.7
    C      32       8.3
    C      64       7.9

Code for plotting:

library(ggplot2)
ggplot(temp, aes(x = type, y = value, fill = type)) + 
  geom_bar(stat = "identity") + 
  facet_grid( . ~ items)

=======================

Edit:

According to Joran's solution, setting scales = "free_x" is doing what I want. However, the widths of the bars become very large under the item numbers 32 and 64. Please help me to make the widths even for all the bars.

ggplot(temp, aes(x = type, y = value, fill = type)) + 
  geom_bar(stat = "identity") + 
  facet_grid( . ~ items, scales = "free_x")

解决方案

Simply following the directions given by joran and Etienne Low-Décarie to close this old unanswered question. Please up-vote joran and Etienne Low-Décarie.

Also, please note Roman Luštrik's valuable comment above "I hope you have a good reason to do this. The white space is very informative, it gives the reader a head's up that those levels have value of 0 (which is still a value)."

# data
temp <- structure(list(type = structure(c(1L, 2L, 3L, 2L, 3L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), items = c(16L, 16L, 16L, 32L, 32L, 
64L), value = c(6.3, 8.3, 7.9, 7.7, 8.3, 7.9)), .Names = c("type", 
"items", "value"), class = "data.frame", row.names = c(NA, -6L
))

# plot
library(ggplot2)
ggplot(temp, aes(type, value, fill = type)) + 
  geom_bar(stat = "identity") + 
  facet_grid( . ~ items, scales = "free_x", space = "free") 

这篇关于在小平面图中省略x轴级别而不显示数据,并更改小节的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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