在ggplot2中的栏之间放置刻度线

时间:2016-01-22 01:13:23

标签: r ggplot2

使用diamonds包中的ggplot2数据集,我可以生成以下图表。

library(ggplot2)
library(dplyr)

diamond.summary <- 
  diamonds %>%
  mutate(carat = ifelse(runif(nrow(.)) < 0.05, NA_real_, carat)) %>%
  group_by(carat_quintile = ntile(carat, 5)) %>%
  summarise(avg_price = mean(price))

diamond.summary %>%
  filter(!is.na(carat_quintile)) %>%
  ggplot(aes(carat_quintile, avg_price)) + 
  geom_bar(stat = "identity", 
           color = "black",
           width = 1) + 
  scale_x_continuous("Carat percentile",
                     breaks = 1:6 - 0.5,
                     labels = seq(0,100, by = 20)) + 
  scale_y_continuous(expand = c(0,0),
                     limits = c(0, 1.1* max(diamond.summary$avg_price)))

enter image description here

到目前为止,这么容易。但是,我还想在图表旁边显示缺失条目的平均价格。类似于以下内容: enter image description here

diamond.summary %>%
  mutate(Facet = is.na(carat_quintile),
         carat_quintile_noNA = ifelse(Facet, "Unknown", carat_quintile)) %>%
  ggplot(aes(x = carat_quintile_noNA, y = avg_price, fill = Facet)) + 
  geom_bar(stat = "identity") + 
  facet_grid(~Facet, scales = "free_x", space = "free_x") + 
  scale_x_discrete(breaks = (0:6) - 0.5)

然而,当我尝试使用scale_x_continuous执行相同的技巧时,我收到错误Discrete value supplied to continuous scale。例如,当我尝试使用scale_x_discrete(breaks = c(0:6 + 0.5))时,轴标记和标签消失。

我的问题是,如何使用第一个面板中的刻度线和上面第一个图表中的刻度标记获得上面相同的刻面图表?关于图表设计的建议可能是一个可接受的解决方案,但我不认为这样的所有问题都可以通过重新设计来解决。

2 个答案:

答案 0 :(得分:5)

诀窍是将您的因子转换为数字,为未知数量指定幻数。 (ggplot2不会使用真NA个值绘制条形图。)然后使用scale_x_continuous

diamond.summary %>%
  mutate(Facet = is.na(carat_quintile),
         carat_quintile_noNA = ifelse(Facet, "Unknown", carat_quintile),
         ## 
         ## 99 is a magic number.  For our plot, it just has
         ## to be larger than 5. The value 6 would be a natural
         ## choice, but this means that the x tick marks would 
         ## overflow ino the 'unknown' facet.  You could choose
         ## choose 7 to avoid this, but any large number works.  
         ## I used 99 to make it clear that it's magic.
         numeric = ifelse(Facet, 99, carat_quintile)) %>%

  ggplot(aes(x = numeric, y = avg_price, fill = Facet)) + 
  geom_bar(stat = "identity", width = 1) + 
  facet_grid(~Facet, scales = "free_x", space = "free_x") + 
  scale_x_continuous(breaks = c(0:5 + 0.5, 99),
                     labels = c(paste0(c(0:5) * 20, "%"), "Unknown"))

enter image description here

答案 1 :(得分:0)

一种解决方案是采用不同的方法,并使用position_nudge重新定位而不是刻度。

library(ggplot2)
library(dplyr)

diamond.summary <- 
  diamonds %>%
  mutate(carat = ifelse(runif(nrow(.)) < 0.05, NA_real_, carat)) %>%
  group_by(carat_quintile = ntile(carat, 5)) %>%
  summarise(avg_price = mean(price))

# nudge bars to the left
diamond.summary %>%
  filter(!is.na(carat_quintile)) %>%
  ggplot(aes(carat_quintile, avg_price)) + 
  geom_bar(stat = "identity", 
           color = "black",
           width = 1,
           position=position_nudge((x=-1))) + 
  scale_x_continuous("Carat percentile",
                     breaks = 1:6 - 0.5,
                     labels = seq(0,100, by = 20)) + 
  scale_y_continuous(expand = c(0,0),
                     limits = c(0, 1.1* max(diamond.summary$avg_price)))

nudge bars left

# nudge bars to the right
diamond.summary %>%
  filter(!is.na(carat_quintile)) %>%
  ggplot(aes(carat_quintile, avg_price)) + 
  geom_bar(stat = "identity", 
           color = "black",
           width = 1,
           position=position_nudge((x=1))) + 
  scale_x_continuous("Carat percentile",
                     breaks = 1:6 - 0.5,
                     labels = seq(0,100, by = 20)) + 
  scale_y_continuous(expand = c(0,0),
                     limits = c(0, 1.1* max(diamond.summary$avg_price)))

nudge bars right