Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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 堆积条形图_R_Ggplot2_Geom Bar - Fatal编程技术网

R 堆积条形图

R 堆积条形图,r,ggplot2,geom-bar,R,Ggplot2,Geom Bar,我想使用ggplot2和geom_栏创建一个堆叠图表 以下是我的原始数据: Rank F1 F2 F3 1 500 250 50 2 400 100 30 3 300 155 100 4 200 90 10 我想要一个堆叠图,其中x是秩,y是F1,F2,F3中的值 # Getting Source Data sample.data <- read.csv('sample.data.csv')

我想使用ggplot2和geom_栏创建一个堆叠图表

以下是我的原始数据:

Rank F1     F2     F3
1    500    250    50
2    400    100    30
3    300    155    100
4    200    90     10
我想要一个堆叠图,其中x是秩,y是F1,F2,F3中的值

# Getting Source Data
  sample.data <- read.csv('sample.data.csv')

# Plot Chart
  c <- ggplot(sample.data, aes(x = sample.data$Rank, y = sample.data$F1))
  c + geom_bar(stat = "identity")
#获取源数据

sample.data您需要将数据转换为长格式,并且不应在
aes
中使用
$

DF <- read.table(text="Rank F1     F2     F3
1    500    250    50
2    400    100    30
3    300    155    100
4    200    90     10", header=TRUE)

library(reshape2)
DF1 <- melt(DF, id.var="Rank")

library(ggplot2)
ggplot(DF1, aes(x = Rank, y = value, fill = variable)) + 
  geom_bar(stat = "identity")

DF您需要
melt
将数据帧转换为所谓的长格式:

require(reshape2)
sample.data.M <- melt(sample.data)
require(重塑2)
sample.data.M你说:

也许我的data.frame格式不好

是的,这是真的。您的数据采用的是格式,您需要将其置于格式。一般来说,长格式更适合于变量比较

使用
重塑2
例如,您可以使用
melt
进行此操作:

dat.m <- melt(dat,id.vars = "Rank") ## just melt(dat) should work
但是使用
lattice
barchart
智能公式表示法,您不需要重塑数据,只需执行以下操作:

barchart(F1+F2+F3~Rank,data=dat)

基于罗兰的答案,使用
tidyr
将数据从宽改长:

library(tidyr)
library(ggplot2)

df <- read.table(text="Rank F1     F2     F3
1    500    250    50
2    400    100    30
3    300    155    100
4    200    90     10", header=TRUE)

df %>% 
  gather(variable, value, F1:F3) %>% 
  ggplot(aes(x = Rank, y = value, fill = variable)) + 
  geom_bar(stat = "identity")
library(tidyr)
图书馆(GG2)
df%
聚集(变量,值,F1:F3)%>%
ggplot(aes(x=秩,y=值,填充=变量))+
几何图形栏(stat=“identity”)

这个问题每天都会被问到basis@user2209016请查看文档:。它回答了很多常见的问题。在我看来,上面文档的链接是一个开始学习ggplot的好地方。例如,知道“美学贴图…只有在覆盖绘图默认值时才需要在图层级别设置”对初学者没有帮助。我发现烹饪书的网页更容易访问。谢谢,这和所有其他答案一起帮助了我。一般来说,您喜欢在ggplot2上使用晶格吗?好问题。简而言之,
ggeplot2
aes是无与伦比的(将数据绑定到绘图功能),但有时我发现lattice
panel
非常有用,并且比
ggplot2
@CMichael No>更容易与
ggplot2
包集成,因此,所有答案都指向
ggplot2
而不是
lattice
,这是正常的。但我承认关于
ggplot2
的问题比“晶格”的问题更多`
ggplot(dat.m, aes(x = Rank, y = value,fill=variable)) +
    geom_bar(stat='identity')
barchart(F1+F2+F3~Rank,data=dat)
library(tidyr)
library(ggplot2)

df <- read.table(text="Rank F1     F2     F3
1    500    250    50
2    400    100    30
3    300    155    100
4    200    90     10", header=TRUE)

df %>% 
  gather(variable, value, F1:F3) %>% 
  ggplot(aes(x = Rank, y = value, fill = variable)) + 
  geom_bar(stat = "identity")