如何使用 y 轴上的 x 和 y 列在 x 轴上绘制 Row.names? [英] How to plot Row.names on x axis with x and y columns on y axis?
问题描述
我有 3 列,Row.names
、x
和 y
.
I have 3 columns, Row.names
, x
, and y
.
如何在 x 轴上绘制 Row.names
,在 y 轴上绘制 x
和 y
,以比较 Row.names
代码>x vs y
?
How do I plot Row.names
on the x axis with x
and y
on the y axis, to compare the lines of x
vs y
?
Row.names x y
1 bare_nuclei NA NA
2 bland_chromatin 5.979253 2.100437
3 clump_thickness 7.195021 2.956332
4 marginal_adhesion 5.547718 1.364629
5 mitoses 2.589212 1.063319
6 normal_nucleoli 5.863071 1.290393
7 single_eipthelial 5.298755 2.120087
8 uniformity_cell_shape 6.560166 1.443231
9 uniformity_cell_size 6.572614 1.325328
推荐答案
让我们使用 ggplot2
:
R/ggplot2 需要具有长"格式的数据(意味着每行一个观察)以创建多种类型的图表.
R/ggplot2 needs to have the data in "long" format (meaning one observation per row) to create many types of graphs.
我们使用 melt
进行转换,使用 Row.names
作为 id.vars
:melt(data,id.vars="Row.names")
.然后我们将行名称分配给 x 轴,将 melt
生成的列称为 value
分配给 y 值.最后,我们使用 geom_bar
为 x 和 y 值着色,并使用 position="dodge"
将它们分成单独的条.
We use melt
to make that transformation, using Row.names
as the id.vars
: melt(data,id.vars="Row.names")
. Then we assign the row names to the x axis, and the column generated by melt
, called value
to the y values. Finally, we use geom_bar
to color your x and y values, and split them into separate bars, using position="dodge"
.
require(ggplot2)
require(reshape2)
df1 <- melt(data,"Row.names")
g1 <- ggplot(df1, aes(x = Row.names, y=value)) +
geom_bar(aes(fill=variable),stat="identity", position ="dodge") +
theme_bw()+
theme(axis.text.x = element_text(angle=-40, hjust=.1))
这篇关于如何使用 y 轴上的 x 和 y 列在 x 轴上绘制 Row.names?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!