Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用geom_bar仅绘制最高值和最低值_R - Fatal编程技术网

使用geom_bar仅绘制最高值和最低值

使用geom_bar仅绘制最高值和最低值,r,R,我试图在条形图上绘制每个国家的GDP。国家名称在x轴上,GDP值在y轴上。然而,有很多国家,我希望柱状图只显示前3个GDP和后3个GDP,在这两者之间,我希望一些点或一些东西表明在这两者之间还有其他国家。我该怎么做呢?1)如果你给出一个玩具数据集,你会得到更快更好的答案 2) 将“点或其他东西”放在绘图上可能会让数据可视化的人有点畏缩。你基本上是在暗示x轴的不连续性,这是一件很常见的事情,但明确排除在ggplot之外 (见此处: 在这里: ) 但是,同样的讨论也提出了解决问题的方法。一种方法可能

我试图在条形图上绘制每个国家的GDP。国家名称在x轴上,GDP值在y轴上。然而,有很多国家,我希望柱状图只显示前3个GDP和后3个GDP,在这两者之间,我希望一些点或一些东西表明在这两者之间还有其他国家。我该怎么做呢?

1)如果你给出一个玩具数据集,你会得到更快更好的答案
2) 将“点或其他东西”放在绘图上可能会让数据可视化的人有点畏缩。你基本上是在暗示x轴的不连续性,这是一件很常见的事情,但明确排除在ggplot之外 (见此处: 在这里: )

但是,同样的讨论也提出了解决问题的方法。一种方法可能是这样:

library(tidyverse)
library(patchwork)
hm_rows <- 50
hm_selected <- 3
toydata <- data.frame(country=paste("Country",1:hm_rows) ,GDP=runif(hm_rows,0,5000))%>%
    arrange(desc(GDP))%>%
    filter(row_number()<=hm_selected | row_number()>(hm_rows-hm_selected))%>%droplevels

toydata$status <- rep(c("Highest","Lowest"),each=hm_selected)

ggplot(toydata%>%filter(status=="Highest"),aes(x=country,y=GDP))+
    geom_bar(stat="identity")+
    ggtitle("Highest")+
    ylim(c(0,max(toydata$GDP)))+
    ggplot(toydata%>%filter(status=="Lowest"),aes(x=country,y=GDP))+
    geom_bar(stat="identity")+
    ggtitle("Lowest")+
    ylim(c(0,max(toydata$GDP)))+
    theme(#possibly questionable, but tweaks the results closer to the single-graph requested:
        axis.text.y=element_blank(),
        axis.ticks=element_blank()
    )+ylab("")
库(tidyverse)
图书馆(拼凑)
hm_行%droplevels
toydata$状态%过滤器(状态=“最高”),aes(x=国家,y=GDP))+
几何图形栏(stat=“identity”)+
标题(“最高”)+
ylim(c(0,最大值(toydata$GDP)))+
ggplot(toydata%>%过滤器(状态=“最低”),aes(x=国家,y=GDP))+
几何图形栏(stat=“identity”)+
标题(“最低”)+
ylim(c(0,最大值(toydata$GDP)))+
主题(#可能有问题,但将结果调整到更接近请求的单个图形:
axis.text.y=元素_blank(),
axis.ticks=元素_blank()
)+ylab(“”)
1)如果你给出一个玩具数据集,你会得到更快更好的答案
2) 将“点或其他东西”放在绘图上可能会让数据可视化的人有点畏缩。你基本上是在暗示x轴的不连续性,这是一件很常见的事情,但明确排除在ggplot之外 (见此处: 在这里: )

但是,同样的讨论也提出了解决问题的方法。一种方法可能是这样:

library(tidyverse)
library(patchwork)
hm_rows <- 50
hm_selected <- 3
toydata <- data.frame(country=paste("Country",1:hm_rows) ,GDP=runif(hm_rows,0,5000))%>%
    arrange(desc(GDP))%>%
    filter(row_number()<=hm_selected | row_number()>(hm_rows-hm_selected))%>%droplevels

toydata$status <- rep(c("Highest","Lowest"),each=hm_selected)

ggplot(toydata%>%filter(status=="Highest"),aes(x=country,y=GDP))+
    geom_bar(stat="identity")+
    ggtitle("Highest")+
    ylim(c(0,max(toydata$GDP)))+
    ggplot(toydata%>%filter(status=="Lowest"),aes(x=country,y=GDP))+
    geom_bar(stat="identity")+
    ggtitle("Lowest")+
    ylim(c(0,max(toydata$GDP)))+
    theme(#possibly questionable, but tweaks the results closer to the single-graph requested:
        axis.text.y=element_blank(),
        axis.ticks=element_blank()
    )+ylab("")
库(tidyverse)
图书馆(拼凑)
hm_行%droplevels
toydata$状态%过滤器(状态=“最高”),aes(x=国家,y=GDP))+
几何图形栏(stat=“identity”)+
标题(“最高”)+
ylim(c(0,最大值(toydata$GDP)))+
ggplot(toydata%>%过滤器(状态=“最低”),aes(x=国家,y=GDP))+
几何图形栏(stat=“identity”)+
标题(“最低”)+
ylim(c(0,最大值(toydata$GDP)))+
主题(#可能有问题,但将结果调整到更接近请求的单个图形:
axis.text.y=元素_blank(),
axis.ticks=元素_blank()
)+ylab(“”)

以@steveLangsford的解决方案为基础——以(可能)稍微更有原则的方式做事

可能有一种更“整洁”的方式来完成这一部分:

  • 查找GDP类别的断点:
使用面绘制(在面板之间添加一点额外空间以强调轴打断):


以@steveLangsford的解决方案为基础——以(可能)稍微更有原则的方式做事

可能有一种更“整洁”的方式来完成这一部分:

  • 查找GDP类别的断点:
使用面绘制(在面板之间添加一点额外空间以强调轴打断):


注意
devtools::安装\u github(“thomasp85/patchwork”)
。。。为什么要使用拼接而不是普通镶嵌面?普通镶嵌面使x轴在镶嵌面上保持一致。拼图让我在左侧面板中的x轴上显示国家A、B、C,在右侧显示国家x、Y、Z。可能有更好的方法,但这对我来说是最快/最简单的。感谢您对github安装的评论,我忘记了!看看我的解决方案(
scale==“free_x”
)哦,这很好。我不知道,谢谢你指出!注意
devtools::install\u github(“thomasp85/patchwork”)
。。。为什么要使用拼接而不是普通镶嵌面?普通镶嵌面使x轴在镶嵌面上保持一致。拼图让我在左侧面板中的x轴上显示国家A、B、C,在右侧显示国家x、Y、Z。可能有更好的方法,但这对我来说是最快/最简单的。感谢您对github安装的评论,我忘记了!看看我的解决方案(
scale==“free_x”
)哦,这很好。我不知道,谢谢你指出!
toydata <- toydata %>%
  mutate(GDP_cat=cut(GDP,breaks=GDP_breaks,labels=
                       c("Lowest","Mid","Highest")),
         country=reorder(factor(country),GDP)) %>%
  filter(GDP_cat != "Mid") %>%
  droplevels()
ggplot(toydata,aes(x=country,y=GDP,fill=GDP_cat))+
  geom_bar(stat="identity")+
  theme_bw()+
  theme(legend.position="none",
    panel.spacing.x=grid::unit(5,"lines")
  )+xlab("")+
  scale_fill_brewer(palette="Dark2")+
  facet_wrap(~GDP_cat,scale="free_x")