在R中删除ggplot2中的单个x轴刻度标记? [英] Remove a single x-axis tick mark in ggplot2 in R?

查看:856
本文介绍了在R中删除ggplot2中的单个x轴刻度标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot2中制作条形图,出于演示原因,我需要在我的一些条形图之间有空格。我在 scale_x_discrete 中使用了限制来插入空栏,这给了我需要的空间。



在我的模拟数据中,组之间 b c 看起来很完美,但是 a b 在背景中仍然有黑色刻度线和白线。我不需要任何x轴网格线,所以我可以很容易地解决白线的问题,但我无法弄清楚如何摆脱刻度线。



我正在使用R版本3.3.1(2016-06-21) - Bug in Your Hair,在RStudio中工作,代码需要ggplot2

  ###模拟具有与我的
my.data相同结构的数据< - data.frame(x = rep(c(a,b c,d),3),
y = c(e,f,g))

###图形
ggplot (my.data,aes(x = x,fill = y))+
geom_bar(position =fill)+
scale_x_discrete(limits = c(a,,b ,,c,d))

###通过删除所有x个网格线删除背景中的白线
ggplot(my.data,aes(x = x ,fill = y))+
geom_bar(position =fill)+
scale_x_discrete(limits = c(a,,b,,c,d ))+
theme(panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank())

如何删除 a b



如果我需要改变我在条形图之间插入空格的方式,我该怎么做并维护图形结构?



$ b $你可以通过黑客来做你想要的:如果你替换你的空白限制 / code>,第一个值为a,ggplot会在第一次出现时放置该栏,并将下一栏留空。

  my.data< -data.frame(x = rep(c(a,b,c ,d),3),
y = c(e,f,g))

ggplot(my.data,aes(x = x, fill = y))+
geom_bar(position =fill)+
scale_x_discrete(limits = c(a,a,b,a,c, d))



但是, right 分隔变量的方法是通过facetting,这需要一个变量来定义你想要的组,例如

  library(dplyr)

#使用您最喜欢的语法创建
my.data%>%mutate(grp = case_when(。$ x =='a'〜1,
。$ x =='b'〜2,
TRUE〜3))
#> x y grp
#> 1 a e 1
#> 2 b f 2
#> 3 c g 3
#> 4 d e 3
#> 5 a f 1
#> 6 b g 2
#> 7 c e 3
#> 8 d f 3
#> 9 a g 1
#> 10 b e 2
#> 11 c f 3
#> 12 dg 3

您可以将其传递给ggplot进行构造:

  my.data%>%mutate(grp = case_when(。$ x =='a'〜1,
。$ x =='b'〜2,
TRUE〜3))%>%
ggplot(aes(x,fill = y))+
geom_bar(position ='fill ')+
facet_grid(。〜grp,space ='free_x',scales ='free_x')


I'm making a bargraph in ggplot2, and for presentation reasons I need spaces between some of my bars. I'm using limits in scale_x_discrete to insert empty bars, which gives me the spacing I need.

The gap between groups b and c in my mock data looks perfect, but the gap between a and b still has the black tick mark and the white line in the background. I don't need any x axis gridlines, so I can fix the problem of the white line easily enough, but I can't work out how to get rid of the tick mark.

I'm using R version 3.3.1 (2016-06-21) -- "Bug in Your Hair", working in RStudio and the code requires ggplot2

### Mock data with the same structure as mine
my.data <- data.frame(x = rep(c("a", "b", "c", "d"), 3),
                      y = c("e", "f", "g"))

### Make graph
ggplot(my.data, aes(x = x, fill = y)) + 
    geom_bar(position = "fill") +
    scale_x_discrete(limits = c("a", "", "b", "", "c", "d"))

### Remove white line in background by removing all x grid lines
ggplot (my.data, aes(x = x, fill = y)) +  
    geom_bar(position = "fill") +
    scale_x_discrete(limits = c("a", "", "b", "", "c", "d")) + 
    theme(panel.grid.minor.x = element_blank(),
          panel.grid.major.x = element_blank())

How do I remove the black tick mark between a and b?

If I need to change the way I'm inserting spaces between bars, how do I do that and maintain the graph structure?

解决方案

You can do what you're asking through a hack: If you replace your blank limits with the first value "a", ggplot will place the bar at the first occurrence and leave the next ones blank:

my.data <-data.frame (x=rep(c("a", "b", "c", "d"),3),
                      y=c("e", "f", "g"))

ggplot(my.data, aes(x=x, fill = y)) + 
    geom_bar(position = "fill") + 
    scale_x_discrete(limits = c("a", "a", "b", "a", "c", "d"))

However, the right way to separate variables is by facetting, which requires a variable to define the groups you want, e.g.

library(dplyr)

            # create with your favorite grammar
my.data %>% mutate(grp = case_when(.$x == 'a' ~ 1,
                                   .$x == 'b' ~ 2,
                                   TRUE ~ 3))
#>    x y grp
#> 1  a e   1
#> 2  b f   2
#> 3  c g   3
#> 4  d e   3
#> 5  a f   1
#> 6  b g   2
#> 7  c e   3
#> 8  d f   3
#> 9  a g   1
#> 10 b e   2
#> 11 c f   3
#> 12 d g   3

which you can pass to ggplot for facetting:

my.data %>% mutate(grp = case_when(.$x == 'a' ~ 1,
                                   .$x == 'b' ~ 2,
                                   TRUE ~ 3)) %>%
    ggplot(aes(x, fill = y)) + 
    geom_bar(position = 'fill') + 
    facet_grid(. ~ grp, space = 'free_x', scales = 'free_x')

这篇关于在R中删除ggplot2中的单个x轴刻度标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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