Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot Row.names on x axis with x and y columns on y axis?

Tags:

plot

r

I have 3 columns, Row.names, x, and 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
like image 634
Joe Johnson Avatar asked Dec 04 '25 08:12

Joe Johnson


1 Answers

Let's use ggplot2:

R/ggplot2 needs to have the data in "long" format (meaning one observation per row) to create many types of graphs.

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))

enter image description here

like image 132
Mako212 Avatar answered Dec 06 '25 00:12

Mako212