是否可以使用GGPLOT制作列图,其中列填充由第三个变量控制?

发布于 2025-02-13 02:28:44 字数 884 浏览 2 评论 0原文

我有一个具有三个连续变量(x,y,z)的数据框架。我想要一个列图,其中x定义列的x轴位置,y定义列的长度,列颜色(y的函数)由z定义。下面的测试代码显示了设置。

`require(ggplot2)
 require(viridis)

 #  Create a dummy data frame

 x <- c(rep(0.0, 5),rep(0.5,10),rep(1.0,15))
 y <- c(seq(0.0,-5,length.out=5),
        seq(0.0,-10,length.out=10),
        seq(0.0,-15,length.out=15))
 z <- c(seq(10,0,length.out=5),
        seq(8,0,length.out=10),
        seq(6,0,length.out=15))

 df <- data.frame(x=x, y=y, z=z)

 pbase <- ggplot(df, aes(x=x, y=y, fill=z))
 ptest <- pbase + geom_col(width=0.5, position="identity") +
                  scale_fill_viridis(option="turbo",
                  limits = c(0,10),
                  breaks=seq(0,10,2.5),
                  labels=c("0","2.5","5.0","7.5","10.0"))
                   
  print(ptest)`

传说具有正确的颜色,但列没有。也许这不是执行此类情节的正确方法。我尝试使用geom_bar()创建具有正确颜色的条形的条,但y值不正确。

I have a data frame with three continuous variables (x,y,z). I want a column plot in which x defines the x-axis position of the columns, y defines the length of the columns, and the column colors (function of y) are defined by z. The test code below shows the set up.

`require(ggplot2)
 require(viridis)

 #  Create a dummy data frame

 x <- c(rep(0.0, 5),rep(0.5,10),rep(1.0,15))
 y <- c(seq(0.0,-5,length.out=5),
        seq(0.0,-10,length.out=10),
        seq(0.0,-15,length.out=15))
 z <- c(seq(10,0,length.out=5),
        seq(8,0,length.out=10),
        seq(6,0,length.out=15))

 df <- data.frame(x=x, y=y, z=z)

 pbase <- ggplot(df, aes(x=x, y=y, fill=z))
 ptest <- pbase + geom_col(width=0.5, position="identity") +
                  scale_fill_viridis(option="turbo",
                  limits = c(0,10),
                  breaks=seq(0,10,2.5),
                  labels=c("0","2.5","5.0","7.5","10.0"))
                   
  print(ptest)`

The legend has the correct colors but the columns do not. Perhaps this is not the correct way to do this type of plot. I tried using geom_bar() which creates a bars with the correct colors but the y-values are incorrect.

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

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

发布评论

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

评论(2

一桥轻雨一伞开 2025-02-20 02:28:44

看起来您有3个X值,每个值出现5、10或15次。您是否希望像现在一样,将条形覆盖在彼此的顶部?如果将alpha = 0.5添加到GEOM_COL调用中,您会看到重叠的条。

另外,您可能会使用躲避来显示彼此相邻的条形,而不是彼此之间。

ggplot(df, aes(x=x, y=y, fill=z, group = z)) + 
  geom_col(width=0.5, position=position_dodge()) +
  scale_fill_viridis_c(option="turbo", # added with ggplot 3.x in 2018
                     limits = c(0,10),
                     breaks=seq(0,10,2.5),
                     labels=c("0","2.5","5.0","7.5","10.0"))

或者您可以按y的顺序绘制数据,以便较小的条出现在顶部,明显:

ggplot(dplyr::arrange(df,y), aes(x=x, y=y, fill=z))+ 
  geom_col(width=0.5, position="identity") +
  scale_fill_viridis_c(option="turbo",
                     limits = c(0,10),
                     breaks=seq(0,10,2.5),
                     labels=c("0","2.5","5.0","7.5","10.0"))

It looks like you have 3 X values that each appear 5, 10, or 15 times. Do you want the bars to be overlaid on top of one another, as they are now? If you add an alpha = 0.5 to the geom_col call you'll see the overlapping bars.

Alternatively, you might use dodging to show the bars next to one another instead of on top of one another.

ggplot(df, aes(x=x, y=y, fill=z, group = z)) + 
  geom_col(width=0.5, position=position_dodge()) +
  scale_fill_viridis_c(option="turbo", # added with ggplot 3.x in 2018
                     limits = c(0,10),
                     breaks=seq(0,10,2.5),
                     labels=c("0","2.5","5.0","7.5","10.0"))

enter image description here

Or you might plot the data in order of y so that the smaller bars appear on top, visibly:

ggplot(dplyr::arrange(df,y), aes(x=x, y=y, fill=z))+ 
  geom_col(width=0.5, position="identity") +
  scale_fill_viridis_c(option="turbo",
                     limits = c(0,10),
                     breaks=seq(0,10,2.5),
                     labels=c("0","2.5","5.0","7.5","10.0"))

enter image description here

凌乱心跳 2025-02-20 02:28:44

我通过使用geom_tile()代替geom_col()解决了此问题。

I solved this by using geom_tile() in place of geom_col().

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