【发布时间】:2021-05-07 01:29:27
【问题描述】:
有没有使用 for 循环绘制 ggplot 的有效方法? ggplot函数中的冒号如何正则表达?
比如我应该如何在代码中正则表达『satisfaction』和『Flight.Distance』?
ggplot(data = t1, aes(x = Flight.Distance))+
geom_histogram(aes(y = ..density.., color =satisfaction, fill =satisfaction), alpha = 0.4, position = "identity") +
geom_density(aes(color = satisfaction), size =1)
plot_data_column_2 = function (data, column1, column2) {
ggplot(data, aes_string(x = column1, fill = column2)) +
geom_bar(position = position_dodge())+
guides(fill=guide_legend(title=sprintf('%s', column2)))+
xlab(sprintf('%s', column2))
}
plot_data_column_2(data = data1, column1 = 'clus_res', column2 = 'Gender')
然而,我无法在 geom_histogram 上复制这种体验。我尝试了一些愚蠢的方法,但得到的输出很糟糕
ggplot(data = t1, aes(x = Flight.Distance))+
geom_histogram(aes(y = ..density.., color =t1[['satisfaction']] ,fill =t1[['satisfaction']]), alpha = 0.4, position = "identity") +
guides(fill=guide_legend(title='satisfaction'))+
geom_density(aes(color = t1[['satisfaction']]), size =1)
因此,我尝试通过删除图例指南并稍后将其重新添加来解决此问题。但是传说已经一去不复返了
ggplot(data = t1, aes(x = t1[['Flight.Distance']]))+
xlab('Flight.Distance')+
geom_histogram(aes(y = ..density.., color =t1[['satisfaction']] ,fill =t1[['satisfaction']]), alpha = 0.4, position = "identity") +
theme(legend.position="none")+
guides(col = guide_legend(ncol = 23))+
geom_density(aes(color = t1[['satisfaction']]), size =1)
【问题讨论】:
-
顺便说一句,最好按总飞行时间或距离对距离进行分层以获得更清晰的视觉表示。这将减少条形图,而不会降低消息的清晰度。