在 ggplot 和 geom_bar 中重复 x 轴值
repeating x axis values in ggplot and geom_bar
我不喜欢使用 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
我想从这个数据集中产生这样的东西:
你能帮帮我吗?我做出来的东西都和我想要的相去甚远
您可以尝试这样的操作,其中 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 不使用 magrittrs pipe-operator 并使用 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")
你肯定在寻找这个结果 - 我使用 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))
我不喜欢使用 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
我想从这个数据集中产生这样的东西:
你能帮帮我吗?我做出来的东西都和我想要的相去甚远
您可以尝试这样的操作,其中 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 不使用 magrittrs pipe-operator 并使用 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")
你肯定在寻找这个结果 - 我使用 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))