基于另一个数据集绘制数据集的基于dplyr的子集时出错
我有两个数据帧基于另一个数据集绘制数据集的基于dplyr的子集时出错,r,ggplot2,dplyr,R,Ggplot2,Dplyr,我有两个数据帧df1和df2。我想根据两个数据集之间的子集生成一个堆叠条形图 df1包含任意两个项目之间的距离 set.seed(123) df1 <- data.frame(col.x=rep(LETTERS[1:6], each=10), col.y=rep(LETTERS[1:6], times=10), dist=runif(60,0,3)) df1 <- df1 %>% filter(!col
df1
和df2
。我想根据两个数据集之间的子集生成一个堆叠条形图
df1
包含任意两个项目之间的距离
set.seed(123)
df1 <- data.frame(col.x=rep(LETTERS[1:6], each=10),
col.y=rep(LETTERS[1:6], times=10),
dist=runif(60,0,3))
df1 <- df1 %>% filter(!col.x==col.y)
summary(df1)
# col.x col.y dist
# A:8 A:8 Min. :0.07384
# B:8 B:8 1st Qu.:0.78300
# C:8 C:8 Median :1.41564
# D:8 D:8 Mean :1.54097
# E:8 E:8 3rd Qu.:2.29776
# F:8 F:8 Max. :2.98281
在第一步中,识别与所选项目(B
)一定距离(2.5
)内的所有项目
colY <- df1 %>% filter(col.x=='B' & dist <= 2.5) %>% select(col.y)
下面的条形图看起来正确,但无法生成堆叠条形图
问题2:上述方法存在什么问题
因为我的原始数据集非常庞大,至少有100万行,所以我想用最快的方法来解决这个问题。您的
fill
变量是连续的。让它成为一个因素。您还可以使用semi_join
来逐个筛选数据帧中的行
datPlot <- df2 %>% semi_join(df1 %>% filter(col.x=='B' & dist <= 2.5),
by = c("col" = "col.y"))
datPlot %>% ggplot(aes(x = ET, fill = as.factor(EV))) + geom_bar()
datPlot%semi_join(df1%>%filter(col.x='B',dist%ggplot(aes(x=ET,fill=as.factor(EV)))+geom_bar()
谢谢,as.factor()
有效!是否有可能以排序方式排列堆叠的条形图?如何排序?它们当前按值排序,如A2
中的值为1、2、8,频率为1、3、2。这些值是否可以在A2
条形图中排序。
datPlot <- df2 %>% filter(col %in% colY$col.y)
datPlot %>% group_by(ET) %>% ggplot(aes(ET)) + geom_bar(aes(fill = EV))
datPlot <- df2 %>% semi_join(df1 %>% filter(col.x=='B' & dist <= 2.5),
by = c("col" = "col.y"))
datPlot %>% ggplot(aes(x = ET, fill = as.factor(EV))) + geom_bar()