首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除绘图中的多余空间?

如何删除绘图中的多余空间?
EN

Stack Overflow用户
提问于 2021-03-20 06:09:23
回答 1查看 77关注 0票数 1

如何删除图表中的额外空间?scale_y_continuous也无法正常工作。

代码语言:javascript
复制
ggplot(data = data_div_1, aes(x = Initiative, y = Record, fill = Type)) +
    geom_bar(stat = "identity") +
    geom_hline(yintercept = 0, size = 2, colour = 'gray50') +
    scale_x_discrete(limits = rev(data_div_1$Initiative)) + 
    scale_y_continuous(breaks = breaks_values, labels = abs(breaks_values)) +
    coord_flip() +
    theme_economist_white(gray_bg = FALSE) + 
    scale_fill_manual(values = c("#1A476F", "#2D6D66")) +
    labs(title = "Theme 1", x = "", y = "", fill = "") +
    theme(legend.position = "none") 

break_values
-15 -10  -5   0   5  10  15

我必须为七个对象重新制作这个发散图,所有对象都有相似的输出。

如果移除scale_x_discrete(),则会显示正确配合但反转的标签。

代码语言:javascript
复制
dput(data_div_1)
structure(list(Theme = c("Theme 1", "Theme 1", "Theme 1", "Theme 1", 
"Theme 1", "Theme 1", "Theme 1", "Theme 1", "Theme 1", "Theme 1"
), Initiative = c("Initiative 1", "Initiative 2", "Initiative 3", 
"Initiative 4", "Initiative 5", "Initiative 1", "Initiative 2", 
"Initiative 3", "Initiative 4", "Initiative 5"), Type = c("Desirability", 
"Desirability", "Desirability", "Desirability", "Desirability", 
"Feasibility", "Feasibility", "Feasibility", "Feasibility", "Feasibility"
), Record = c(10, 9.5, 11, 11.5, 7.5, -9, -8.5, -9, -8.5, -8.5
), Label = c("Theme 1: Initiative 1", "Theme 1: Initiative 2", 
"Theme 1: Initiative 3", "Theme 1: Initiative 4", "Theme 1: Initiative 5", 
"Theme 1: Initiative 1", "Theme 1: Initiative 2", "Theme 1: Initiative 3", 
"Theme 1: Initiative 4", "Theme 1: Initiative 5")), class = "data.frame", row.names = c(NA, 
-10L))
EN

回答 1

Stack Overflow用户

发布于 2021-03-20 16:13:19

这可以通过rev(unique(...))实现,如下所示:

代码语言:javascript
复制
library(ggplot2)
library(ggthemes)

breaks_values <- c(-15L, -10L, -5L, 0L, 5L, 10L, 15L)

ggplot(data = data_div_1, aes(x = Initiative, y = Record, fill = Type)) +
  geom_bar(stat = "identity") +
  geom_hline(yintercept = 0, size = 2, colour = 'gray50') +
  scale_x_discrete(limits = rev(unique(data_div_1$Initiative))) + 
  scale_y_continuous(breaks = breaks_values, labels = abs(breaks_values)) +
  coord_flip() +
  theme_economist_white(gray_bg = FALSE) + 
  scale_fill_manual(values = c("#1A476F", "#2D6D66")) +
  labs(title = "Theme 1", x = "", y = "", fill = "") +
  theme(legend.position = "none") 

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66716162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档