我不喜欢用Excel来制作我的情节,所以我想使用R代替ggplot2
,如果可能的话。
这是我的数据集,可能它没有采用正确的格式来执行我想要做的事情:
C>A C>G C>T T>A T>C T>G
ACA 0.049915398 0.008460237 0.018612521 0.015228426 0.036379019 0.005922166
ACC 0.015228426 0.003384095 0.010152284 0.005922166 0.005076142 0
ACG 0.014382403 0.005076142 0.010998308 0.007614213 0.013536379 0.001692047
ACT 0.031302876 0.007614213 0.01607445 0.010998308 0.013536379 0.002538071
CCA 0.021150592 0.005076142 0.011844332 0.007614213 0.011844332 0.001692047
CCC 0.027072758 0.002538071 0.009306261 0.005076142 0.004230118 0
CCG 0.014382403 0.001692047 0.009306261 0.005076142 0.008460237 0.000846024
CCT 0.0321489 0.00676819 0.016920474 0.00676819 0.008460237 0.000846024
GCA 0.011844332 0.003384095 0.015228426 0.003384095 0.013536379 0.002538071
GCC 0.008460237 0.004230118 0.010152284 0.007614213 0.011844332 0.003384095
GCG 0.002538071 0.004230118 0.010998308 0.009306261 0.010998308 0.003384095
GCT 0.012690355 0.005076142 0.010998308 0.003384095 0.005076142 0.000846024
TCA 0.030456853 0.011844332 0.013536379 0.011844332 0.017766497 0.001692047
TCC 0.026226734 0.00676819 0.017766497 0.002538071 0.004230118 0.002538071
TCG 0.011844332 0.000846024 0.009306261 0.003384095 0.011844332 0.000846024
TCT 0.03891709 0.016920474 0.020304569 0.008460237 0.019458545 0.00676819
从这个数据集我想产生这样的东西:
你能帮帮我吗?我生产的一切都远非我想要的。答案 0 :(得分:1)
您肯定在寻找这个结果 - 我使用tidyr
或reshape2
库来首先塑造数据:
library(reshape2)
df1 = melt(df, id.vars='Gene', variable.name='Class', value.name='Value')
#df1 = gather(df, Class, Value, -Gene) using tidyr
df1 = transform(df1, x=1:nrow(df1))
ggplot(df1, aes(x=x, y=Value, fill=Class)) +
geom_bar(stat="identity") +
scale_x_discrete(labels=df1$Gene) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
数据:强>
df = structure(list(Gene = c("ACA", "ACC", "ACG", "ACT", "CCA", "CCC",
"CCG", "CCT", "GCA", "GCC", "GCG", "GCT", "TCA", "TCC", "TCG",
"TCT"), C.A = c(0.049915398, 0.015228426, 0.014382403, 0.031302876,
0.021150592, 0.027072758, 0.014382403, 0.0321489, 0.011844332,
0.008460237, 0.002538071, 0.012690355, 0.030456853, 0.026226734,
0.011844332, 0.03891709), C.G = c(0.008460237, 0.003384095, 0.005076142,
0.007614213, 0.005076142, 0.002538071, 0.001692047, 0.00676819,
0.003384095, 0.004230118, 0.004230118, 0.005076142, 0.011844332,
0.00676819, 0.000846024, 0.016920474), C.T = c(0.018612521, 0.010152284,
0.010998308, 0.01607445, 0.011844332, 0.009306261, 0.009306261,
0.016920474, 0.015228426, 0.010152284, 0.010998308, 0.010998308,
0.013536379, 0.017766497, 0.009306261, 0.020304569), T.A = c(0.015228426,
0.005922166, 0.007614213, 0.010998308, 0.007614213, 0.005076142,
0.005076142, 0.00676819, 0.003384095, 0.007614213, 0.009306261,
0.003384095, 0.011844332, 0.002538071, 0.003384095, 0.008460237
), T.C = c(0.036379019, 0.005076142, 0.013536379, 0.013536379,
0.011844332, 0.004230118, 0.008460237, 0.008460237, 0.013536379,
0.011844332, 0.010998308, 0.005076142, 0.017766497, 0.004230118,
0.011844332, 0.019458545), T.G = c(0.005922166, 0, 0.001692047,
0.002538071, 0.001692047, 0, 0.000846024, 0.000846024, 0.002538071,
0.003384095, 0.003384095, 0.000846024, 0.001692047, 0.002538071,
0.000846024, 0.00676819)), .Names = c("Gene", "C.A", "C.G", "C.T",
"T.A", "T.C", "T.G"), class = "data.frame", row.names = c(NA,
-16L))
答案 1 :(得分:0)
你可以尝试这样的事情,其中df是你的数据帧:
library(ggplot2)
library(tidyr)
library(magrittr)
df$rowvalues <- rownames(df)
gather(df, Variables, Values, 2:ncol(df)) %>%
ggplot(aes(x = rowvalues, y = Values, fill = Variables) +
geom_bar(stat = "identity")
版本2,不使用magrittr管道运算符并使用reshape2而不是tidyr:
library(ggplot2)
library(reshape2)
df$rowvalues <- rownames(df)
df.long <- melt(df, id.vars = ("rowvalues"),
variable.name = "variable_name",
value.name = "value_name")
ggplot(df.long, aes(x = rowvalues, y = value_name, fill = variable_name) +
geom_bar(stat = "identity")