在分类和数值数据框架上迭代并在R中绘制条形图
我有一个数字数据帧调用a
和一个cancoriacal一个调用b
对于每两个变量(一个来自b
的变量,一个来自a
的变量),我想绘制条形图。
我的尝试:
y <- unlist(lapply(D, Negate(is.numeric)))
x <- unlist(lapply(D, is.numeric))
A <- D[,x]
B <- D[,y]
A <- as.data.frame(A)
B <- as.data.frame(B)
for(i in (1:ncol(A)))
{
for(j in (1:ncol(B)))
{
ggplot(D, aes(x = A[,i], y = B[,j])) +
geom_bar(fill='red')
}
}
但是什么都没有绘制任何东西。我不确定是否有必要将每个曲线保存在向量中,然后用另一个绘制它们。任何建议都很棒!
I have a numeric data frame call A
and a categoriacal one call B
For every two variables (one from B
and one from A
) I want to plot a bar chart.
My try:
y <- unlist(lapply(D, Negate(is.numeric)))
x <- unlist(lapply(D, is.numeric))
A <- D[,x]
B <- D[,y]
A <- as.data.frame(A)
B <- as.data.frame(B)
for(i in (1:ncol(A)))
{
for(j in (1:ncol(B)))
{
ggplot(D, aes(x = A[,i], y = B[,j])) +
geom_bar(fill='red')
}
}
But is not plotting anything. I'm not sure if it's necessary to save each plot in a vector and then plot them with another for. Any suggestions would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
print()
函数中包装GGPLOT语句以在循环中生成输出:You will need to wrap the ggplot statement inside the
print()
function to generate output while inside a loop:我认为您需要用
Geom_bar()
用geom_col()
或添加stat = station = Insterity
geom_bar(geom_bar()。您还可以避免使用循环使用
pmap
from packagepurrr
:我还应该补充说,您可以分配
的结果pmap
到对象(例如p
),然后使用p [[k]]
调用每个图表。I think you need to replace
geom_bar()
withgeom_col()
or to addstat = identity
insidegeom_bar()
.You can also avoid using the
for
loop by usingpmap
from packagepurrr
:I should also add that you can assign the result of
pmap
to an object (e.g.p
) and then call each charts usingp[[k]]
.