Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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
R 带有多个x变量的ggplot2图形?_R_Ggplot2 - Fatal编程技术网

R 带有多个x变量的ggplot2图形?

R 带有多个x变量的ggplot2图形?,r,ggplot2,R,Ggplot2,我需要有关ggplot2的R图形问题的帮助。 让我们举一个例子: date <- c("oct", "dec") min.national <- c(17, 20) min.international <- c(11, 12) min.roaming <- c(5, 7) mb.national <- c(115, 150) mb.international <- c(72, 75) mb.roaming <- c(30, 40) df <-

我需要有关ggplot2的R图形问题的帮助。 让我们举一个例子:

date <- c("oct", "dec")

min.national <- c(17, 20)
min.international <- c(11, 12)
min.roaming <- c(5, 7)

mb.national <- c(115, 150)
mb.international <- c(72, 75)
mb.roaming <- c(30, 40)

df <- data.frame(min.national, min.international, min.roaming, mb.national, mb.international, mb.roaming)

date
df我很感激这里可能存在语言挑战,而且听起来您刚刚开始使用ggplot2,所以不确定如何开始使用它,所以我希望您觉得这很有用

将分钟和mb分开处理是有意义的;他们是不同的单位。所以我将以分钟为例。据我所知,通过正确的方法和tidyr库,您试图实现的目标很容易实现

library(tidyr)
library(ggplot2)
#first get your data in a data frame
min.df <- data.frame(national = min.national, international = min.international, roaming = min.roaming, month = date)
#now use the tidyr function to create a long data frame, you should recognize that this gives you a data structure readily suited to what you want to plot
min.df.long <- gather(min.df, "region", "minutes", 1:3)
ggplot(min.df.long) + geom_bar(aes(x = region, y = minutes, fill = month), stat = "identity")
关键参数是position关键字,其余的只是为了让它更整洁。

不清楚的是您自己尝试过什么。对不起,您的意思是什么?对不起,我尝试过,但很难做到,我可以在R或excel上向您展示我的基本图形?非常感谢,再问一个问题,如何按日期并排制作条形图?我认为这更容易理解(附言:我对ggplot并不陌生,但我在使用tidyr和重塑时遇到了一些问题…)请参阅编辑后的答案,我认为,它会按照您的要求执行;这是一个简单的geom_bar位置选项。欢迎您,如果这回答了您的问题,请将其标记为已回答。
library(tidyr)
library(ggplot2)
#first get your data in a data frame
min.df <- data.frame(national = min.national, international = min.international, roaming = min.roaming, month = date)
#now use the tidyr function to create a long data frame, you should recognize that this gives you a data structure readily suited to what you want to plot
min.df.long <- gather(min.df, "region", "minutes", 1:3)
ggplot(min.df.long) + geom_bar(aes(x = region, y = minutes, fill = month), stat = "identity")
ggplot(min.df.long) + geom_bar(aes(x = region, y = minutes, fill = factor(month, levels = c("oct", "dec"))), position = "dodge", stat = "identity") + labs(fill = "month")