展开y比例但限制y轴线

时间:2017-04-07 13:32:40

标签: r ggplot2

我的目标是在ggplot2中制作一个简单的柱形图,如下图所示(在Excel中制作): enter image description here

我发现的是,通过这样的示例数据(其中一个百分比值非常接近100%),我在ggplot2中绘制这些数据的选项留下了一些不足之处。特别是,我还没有找到一种方法让以下两个简单的事情一起发生:

1)使y轴线终止于100%

2)使每个栏上的百分比标签可见

为了解决这个问题,我尝试了scale_y_continuous()的不同论点,但没有找到同时满足上述两个目标的方法。您可以在下面的示例图和代码中看到这一点。

我的问题是:如何扩展y比例,以便我的每个数据点上的百分比标签可见,但y轴线以100%结束?

library(dplyr)
library(ggplot2)
library(scales)


example_df <- data_frame(Label = c("A", "B"),
                         Percent = c(0.5, 0.99))

example_plot <- example_df %>%
      ggplot(aes(x = Label, y = Percent)) +
         geom_bar(stat = "identity",
                  fill = "dodgerblue4", width = .6) +
         geom_text(aes(label = percent(Percent)),
                   size = 3, vjust = -0.5) +
         scale_x_discrete(NULL, expand = c(0, .5)) +
         theme_classic()

使用所需的y轴线绘制,但在条上显示不可见的标签

当我将scale_y_continuous()的限制设置为c(0,1)时,会发生以下情况:

enter image description here

example_plot +
     scale_y_continuous(NULL, limits = c(0, 1.0), breaks = seq(0, 1, .2),
                        labels = function(x) scales::percent(x),
                        expand = c(0, 0)) +
     labs(title = "Y axis line looks perfect, but the label over the bar is off")

绘制y轴线太长,但可见标签超过条

以下是我将scale_y_continuous()的限额设置为c(0,1.05)时会发生的情况:

enter image description here

example_plot +
     scale_y_continuous(NULL, limits = c(0, 1.05), breaks = seq(0, 1, .2),
                        labels = function(x) scales::percent(x),
                        expand = c(0, 0)) +
     labs(title = "Y axis line is too long, but the label over the bar is visible")

1 个答案:

答案 0 :(得分:1)

您可以删除常规轴线,然后使用example_df %>% ggplot(aes(x = Label, y = Percent)) + geom_bar(stat = "identity", fill = "dodgerblue4", width = .6) + geom_text(aes(label = percent(Percent)), size = 3, vjust = -0.5) + scale_x_discrete("", expand = c(0, .5)) + scale_y_continuous("", breaks = seq(0, 1, .2), labels = percent, limits=c(0,1.05), expand=c(0,0)) + theme_classic() + theme(axis.line.y=element_blank()) + geom_segment(x=.5025, xend=0.5025, y=0, yend=1.002) 创建一个新轴:

library(grid)

p = example_df %>%
   ggplot(aes(x = Label, y = Percent)) +
    geom_bar(stat = "identity", fill = "dodgerblue4", width = .6) +
    geom_text(aes(label = percent(Percent)), size = 3, vjust = -0.5) +
    scale_x_discrete("", expand = c(0, .5)) +
    scale_y_continuous("", breaks = seq(0, 1, .2), labels = percent, limits=c(0,1), 
                      expand=c(0,0)) +
    theme_classic() +
    theme(plot.margin=unit(c(10,0,0,0),'pt'))

# Turn off clipping
pg <- ggplot_gtable(ggplot_build(p))
pg$layout$clip[pg$layout$name=="panel"] <- "off"
grid.draw(pg) 

enter image description here

回复您的评论:即使它在情节区域之外,仍然会绘制99%的标签,但它会被剪裁&#34;,这意味着外面的情节元素情节区域被掩盖。所以,另一个选择,仍然是hacky,但比我原来的答案更少hacky,是关闭剪辑,以便标签出现:

@Injectable()
export class CaseService {

  constructor(private _http: Http) {}

  public GetCase() {

     const url = 'http://localhost:55130/api/case?caseId=123';
     let headers = new Headers();
     headers.append('Content-Type', 'application/json');
     return this._http.get(url,{headers:headers})
             .map(response => {
                return response.json();
             });
  }
}

enter image description here