I have a dataset with 7 columns which contains first row as a names and a remaining 6 columns are values. I need to create a density plot for the data. Here's a sample dataset:
structure(list(`Majority protein IDs` = c("P19882", "P38999",
"P34227", "Q12335", "P40893", "P33416", "Q04947", "Q12074", "P38116",
"P32598", "P38221", "P52910", "P0CX51;P0CX52", "Q02805", "P50861",
"Q02821", "P53691", "P19414", "Q04013", "Q07799", "P47039"),
A1 = c(964860000, 46301000, 135090000, 126280000, 96191000,
91552000, 75231000, 76684000, 6150500, 21904000, 55134000,
231070000, 259060000, 7304500, 180320000, 22025000, 79510000,
677560000, 5994400, 18950000, 3468200), A2 = c(1074500000,
42888000, 125590000, 1.27e+08, 68911000, 93280000, 66457000,
69568000, 4848600, 20342000, 52573000, 242230000, 286970000,
5770200, 190140000, 22305000, 87598000, 673050000, 6459300,
21634000, 3115400), A3 = c(1021200000, 56550000, 117190000,
124730000, 69026000, 80712000, 55917000, 71989000, 5390500,
19299000, 51348000, 213050000, 254080000, 6976000, 181280000,
20951000, 83504000, 664590000, 6155300, 21158000, 2940600
), B1 = c(846340000, 34713000, 95928000, 121440000, 16256000,
63182000, 46570000, 59008000, 1465600, 24838000, 48464000,
176140000, 175320000, 8738500, 120870000, 24496000, 75694000,
524780000, 4373900, 16281000, 5801700), B2 = c(921740000,
35240000, 109990000, 111250000, 39047000, 74125000, 51027000,
65971000, 3027100, 36529000, 39712000, 179630000, 218240000,
8092500, 152610000, 26032000, 62681000, 587100000, 4887000,
18454000, 3811100), B3 = c(908710000, 38475000, 106400000,
113590000, 36745000, 74175000, 48912000, 64201000, 3073300,
39069000, 40650000, 206350000, 220280000, 8673100, 154260000,
30254000, 61422000, 617900000, 5561000, 17102000, 5486600
)), row.names = c(NA, -21L), class = c("tbl_df", "tbl", "data.frame"
))
I used the below function for making the density plot:
Densityplot_data <- function (data){
variable <- value <- NULL
new_data <- as.data.frame(data)
new_data <- new_data[,-1]
options(warn = -1)
density_plot <- new_data %>%
tibble::as.tibble() %>%
tidyr::gather(variable, value) %>%
ggplot2::ggplot(ggplot2::aes(x=value) ) +
ggplot2::geom_density (fill= "#69b3a2") +
ggplot2::facet_wrap(~variable, scales="free", nrow = 3, dir = "v")+
ggplot2::theme_gray()+
ggplot2::theme(text = ggplot2::element_text(size = 20))
return(plotly::ggplotly(density_plot))
}
Densityplot_data(testdata)
Which returns the plot below:
But I need to get the columns in the vertical order like,
A1 B1
A2 B2
A3 B3
Densityplot_data <- function (data){
variable <- value <- NULL
new_data <- as.data.frame(data)
new_data <- new_data[,-1]
options(warn = -1)
density_plot <- new_data %>%
tibble::as.tibble() %>%
tidyr::gather(variable, value) %>%
dplyr::mutate(order_hlp = as.numeric(gsub("[A-Za-z]", "",variable))) %>%
dplyr::arrange(order_hlp) %>%
dplyr::mutate(variable_fac = factor(variable, levels = unique(variable))) %>%
ggplot2::ggplot(ggplot2::aes(x=value) ) +
ggplot2::geom_density (fill= "#69b3a2") +
ggplot2::facet_wrap(~variable_fac, scales="free", nrow = 3)+
ggplot2::theme_gray()+
ggplot2::theme(text = ggplot2::element_text(size = 20))
return(plotly::ggplotly(density_plot))
}
Densityplot_data(testdata)