简体   繁体   English

在R中的单个图上绘制两个变量的条形图和第三个变量的线形图

[英]Plotting a bar plot of two variables and a line plot of third variable on a single plot in R

I am trying to plot a bar plot (with position ='identity', stat = 'identity' ) and a line plot on the same plot against months using ggplot2 package in R. 我正在尝试在R中使用ggplot2包绘制一个条形图(带有position ='identity', stat = 'identity' )和一个线形图相对于月份绘制在同一图上。

My data consists of 3 mainly concerned values 我的数据包含3个主要关注的值

1- Global irradiance 2- Diffuse irradiance 3- Temperature 1-整体辐照度2-漫辐照度3-温度

To make a bar plot, I have merged the values of Global and Diffuse irradiances in one molten dataframe. 为了绘制条形图,我在一个熔融数据框中合并了“全局”和“漫射”辐照度的值。 Whereas I have kept temperature values separate in an initial dataframe as I don't want it's bar plot with the irradiance values. 鉴于我不希望温度数据在初始数据帧中分开,因为我不希望它是带有辐照度值的条形图。

Now problem is I am unable to make ggplot from two different dataframes. 现在的问题是我无法从两个不同的数据帧制作ggplot。 The error I am getting using this approach is 我使用这种方法得到的错误是

Error: ggplot2 doesn't know how to deal with data of class uneval 错误:ggplot2不知道如何处理uneval类的数据

even though class of both datasets is dataframe now. 即使两个数据集的类现在都是数据框。

On the other hand, I am confused on how to include my values in a single dataframe that I can plot all them together as bar and line plots. 另一方面,我对如何将值包含在单个数据帧中感到困惑,因为我可以将所有值绘制为条形图和折线图。

My piece of code for data is: 我的数据代码是:

library(ggplot2)
library(officer)
library(reshape2)

data <- "U://30-Power & Water//25 Renewables//WORK//Data//PVPlanner//PVPlanner.csv"

data <- read.table(data,skip = 36, header = T,  sep=";")
data <- data.frame(data[1:12,])
data$Month <- factor(data$Month,levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

whereas my data looks like: 而我的数据如下所示:

Month GHId GHIm Diffd Diffm  T24
1    Jan 3.27  101  0.92    29 13.3
2    Feb 3.92  110  1.23    34 13.3
3    Mar 5.30  164  1.58    49 13.9
4    Apr 6.18  185  1.89    57 14.9
5    May 6.93  215  2.00    62 16.4
6    Jun 7.53  226  1.80    54 18.4
7    Jul 7.42  230  1.87    58 21.1
8    Aug 7.06  219  1.58    49 22.0
9    Sep 5.97  179  1.39    42 21.3
10   Oct 4.50  140  1.26    39 18.7
11   Nov 3.53  106  0.99    30 15.9
12   Dec 2.90   90  0.86    27 13.3

The code for making a molten dataframe out of my data is: 用于从我的数据中生成熔融数据框的代码是:

dfd <- melt(data[,c('Month','GHId','Diffd')],id.vars = 1)

The code for my ggplot is: 我的ggplot的代码是:

ggplot() + geom_bar(dfd,aes(x = Month,y = value,fill = variable),stat = "identity",position = "identity")+geom_line(data,aes(Month, data[,6]), col=red)

I am trying to make this kind of plot: 我试图做这样的情节: 在此处输入图片说明

I am looking forward to getting guidance on the type of approach that I can take either to work on a single dataframes or to call values from two different dataframes. 我期待获得有关可用于单个数据框或从两个不同数据框调用值的方法类型的指导。

Thanks in advance! 提前致谢! Regards 问候

Ok.. I was able to spot out my mistake, If there are values from two dataframes we need to mentioned properly where exactly data is in our code like data = dfd in both the geom 好的..我能够找出我的错误,如果有两个数据帧中的值,我们需要适当地提及代码中确切的数据在data = dfd ,例如两个geom data = dfd

OR 要么

We need to code using exact arrangement of the code as mentioned in the function documentation! 我们需要使用功能文档中提到的精确排列代码进行编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM