得到一个躲避的条形图?

发布于 2024-12-01 03:08:13 字数 919 浏览 0 评论 0原文

我刚刚遇到了这段代码,它可以让我绘制多个坐标(我知道这在社区中是一个坏主意,但我需要这样做来比较不同的指标)。由于这不能在 ggplot2 中固有地完成,我想知道是否可以调整以下代码来完成我需要的操作:

twoord.plot(2:10,seq(3,7,by=0.5)+rnorm(9),
  1:15,rev(60:74)+rnorm(15),xlab="Sequence",
  ylab="Ascending values",rylab="Descending values",
  main="Plot with two ordinates - bars on the left",
  type=c("bar","l"),lcol=3,rcol=4)

绘制以下内容:

“在此处输入图像描述”

我正在寻找的是并排绘制多个条形图。我尝试用 type=c("bar", "bar") 替换 type=c("bar", "l") ,但这给了我重叠的条形。我想要绘制的确切数据如下:

Category    SubCat  Value
A           Cat1    0.1
B           Cat1    0.2
A           Cat2    0.3
B           Cat2    0.24
A           Cat3    13
B           Cat3    41
A           Cat4    60
B           Cat4    146

我希望(Cat1,Cat2)下的(A,B)的刻度位于左侧,(Cat3,Cat4)下的(A,B)的刻度位于左侧右侧以及 A 和 B 下的所有条形图分别分组在一起。有人可以告诉我该怎么做吗?

I just came across this code that lets me plot multiple ordinates (I know it is a bad notion in the community but I need to do it to compare different metrics). Since this cannot be done inherently in ggplot2, I was wondering if I can adapt the following code to do what I need:

twoord.plot(2:10,seq(3,7,by=0.5)+rnorm(9),
  1:15,rev(60:74)+rnorm(15),xlab="Sequence",
  ylab="Ascending values",rylab="Descending values",
  main="Plot with two ordinates - bars on the left",
  type=c("bar","l"),lcol=3,rcol=4)

plots the following:

enter image description here

What I am looking for is to plot multiple bars side to side. I tried replacing type=c("bar", "l") with type=c("bar", "bar") but that gave me overlapping bars. The exact data I want to plot is the following:

Category    SubCat  Value
A           Cat1    0.1
B           Cat1    0.2
A           Cat2    0.3
B           Cat2    0.24
A           Cat3    13
B           Cat3    41
A           Cat4    60
B           Cat4    146

I want (A,B) under (Cat1,Cat2) to have the scale on the left, and (A,B) under (Cat3, Cat4) to have the scale on the right and all bars under A and B to be grouped together respectively. Can someone please tell me how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-12-08 03:08:13

以下内容可能与您使用 ggplot2 所得到的内容最接近:

d <- read.table(textConnection("Category    SubCat  Value
A           Cat1    0.1
B           Cat1    0.2
A           Cat2    0.3
B           Cat2    0.24
A           Cat3    13
B           Cat3    41
A           Cat4    60
B           Cat4    146"),header = TRUE, sep="")

d$grp <- rep(c('Cat1 - Cat2','Cat3 - Cat4'), each = 4)
ggplot(d,aes(x = Category, y = Value)) + 
    facet_wrap(~grp, scales = "free_y") + 
    geom_bar(aes(fill = SubCat), position = "dodge")

在此处输入图像描述

这通过分面并允许 ggplot2 单独设置轴来实现“双 y 轴”。我确信使用基本图形可以得到更接近您想要的东西,但这需要更多的考虑。

编辑

这是一个使用twoord.plot的技巧。花了相当多的时间,但我从来没有使用过这个功能,所以可能有一个更简单的方法:

par(xaxt = "n") 
twoord.plot(ly = d$Value[1:4],ry = d$Value[5:8], rx = c(9,11,13,15), lx = c(1,3,5,7),
            type = c('bar','bar'), 
            xlim = c(0,16),
            lylim = c(0,0.35),rylim = c(12,150))
par(xaxt="s")           
axis(1, at = c(1,3,5,7,9,11,13,15),
        labels = paste(d$Category,d$SubCat,sep=""),
        las = 3)

The following is probably as close as you're going to get using ggplot2:

d <- read.table(textConnection("Category    SubCat  Value
A           Cat1    0.1
B           Cat1    0.2
A           Cat2    0.3
B           Cat2    0.24
A           Cat3    13
B           Cat3    41
A           Cat4    60
B           Cat4    146"),header = TRUE, sep="")

d$grp <- rep(c('Cat1 - Cat2','Cat3 - Cat4'), each = 4)
ggplot(d,aes(x = Category, y = Value)) + 
    facet_wrap(~grp, scales = "free_y") + 
    geom_bar(aes(fill = SubCat), position = "dodge")

enter image description here

This achieves a "dual y axis" by faceting and allowing ggplot2 to set the axes separately. I'm sure it's possible to get something closer to what you want using base graphics, but it will take some more thought.

EDIT

And here's a hack using the twoord.plot. Took quite a bit of fiddling, but I've never used that function, so there may be an easier way:

par(xaxt = "n") 
twoord.plot(ly = d$Value[1:4],ry = d$Value[5:8], rx = c(9,11,13,15), lx = c(1,3,5,7),
            type = c('bar','bar'), 
            xlim = c(0,16),
            lylim = c(0,0.35),rylim = c(12,150))
par(xaxt="s")           
axis(1, at = c(1,3,5,7,9,11,13,15),
        labels = paste(d$Category,d$SubCat,sep=""),
        las = 3)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文