ggplot-具有多个变量的条形图 [英] ggplot- Bar chart with multiple variables

查看:74
本文介绍了ggplot-具有多个变量的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个条形图,其中包含三个变量:pH,温度和溶解氧.我希望按日期螺母对它们进行分组,很难使图表成为具有正确比例的单独条形图.目前,我的图表无处不在.这是我到目前为止所拥有的:

I am creating a bar chart with three variables, pH, Temp, and Dissolved Oxygen. I would like them to be grouped by date nut am having a hard time getting the chart to be seperate bars with the correct scale. Currently my graph is all over the place. This is what I have so far:

dat.g <- gather(Plaster_2019_Data, type, value, -Date)
ggplot(dat.g, aes(Date, value)) + 
  geom_bar(aes(fill =Date), stat = 'identity', position = "dodge2") 

我希望这些条与pH,温度和溶解氧相对应,并且y轴在1到30的一个范围内.任何帮助将不胜感激!

I would like the bars to correspond with pH, Temp, and Dissolved Oxygen and the y axis to be on one scale from 1-30. Any help would be appreciated!

数据:

Date       Surface  pH     Temperature
May        12.08    8.56    11.16
May        11.68    8.90    8.76
June        8.69    9.07    14.65
June        2.26    7.49    17.51
July        4.54    7.77    23.82
July        2.13    8.17    25.29
August      6.34    8.62    26.50
September   9.33    9.03    24.31
September   10.98   8.58    21.02
September   9.59    8.61    17.33
October     16.07   8.70    10.39
October     9.12    8.07    6.38

推荐答案

如何通过 type 列对图进行构图?

How about faceting your plot by the type column?

ggplot(dat.g, aes(Date, value)) + 
  geom_bar(aes(fill = Date), stat = 'identity', position = "dodge2") +
  facet_grid(.~type) +
  expand_limits(y = c(1,30))

如果您希望条形图在同一平面上,则另一种选择是将 fill = 参数更改为 type ,因为您的x轴已指示月份.

If you want the bars to be on the same plane, another option is to change the fill = argument to type as your x-axis already indicates the months.

ggplot(dat.g, aes(Date, value)) + 
  geom_bar(aes(fill = type), stat = 'identity', position = "dodge2") +
  expand_limits(y = c(1,30))

这篇关于ggplot-具有多个变量的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆