我想用plotly重新创建我的ggplot条形图以使其具有交互性。但是,我无法让 y 轴成为我想要的变量,而是出现以下错误: Error in eval(expr, data, expr_env) : 未找到对象“genome_size_in_Mb”。
我的数据:
dput():
structure(list(Subgroups = c("Jessenii", "fluorescens", "fluorescens",
"gessardii", "gessardii", "fragi", "fragi"), `Species Name + Strain` = c("Pseudomonas umsongensis",
"Pseudomonas fluorescens", "Pseudomonas extremaustralis", "Pseudomonas sp.",
"Pseudomonas fluorescens", "Pseudomonas psychrophila", "Pseudomonas taetrolens"
), Strain = c("GO16", "SBW25", "DSM17835", "Ag1", "R8", "HA4",
"LMG2336"), `refseq assembly` = c("GCF_008824165.1", "GCF_000009225.2",
"GCF_900102035.1", "GCF_000006765.1", "GCF_000297195.3", "GCF_000282975.1",
"GCF_900104825.1"), genome_size_in_Mb = c(7.4, 6.7, 6.7, 6.3,
7, 5.2, 4.9), `chromosome number` = c("2", "1", "1", "1", "1",
"-", "-"), `GC content` = c(59, 60.5, 60.5, 66.5, 61, 56.5, 58
), `Number of CDS genes` = c(6441, 5974, 6000, 5572, 6307, 4666,
4360), `Genes number` = c(6698, 6154, 6228, 6708, 6440, 4823,
4507)), row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"
))
ggplot图表代码:
ggplot(
data = dba,
aes(x = forcats::fct_inorder(Strain),
y = genome_size_in_Mb,
fill = Subgroups)
) +
geom_bar(
position = "dodge",
stat = "identity",
width = 0.5
) +
scale_x_discrete(guide = guide_axis(angle = 90)) +
labs(title = "Genome Sizes Across Strains", x = "Strain", y = "Genome Size in Mb")
以下情节示例在线
library(plotly)
library(dplyr)
fig1 <- db
fig1 <- fig %>% count(Strain, Subgroups)
fig1 <- fig %>% plot_ly(x = ~Strain, y = ~n, color = ~Subgroups)
print(fig1)
有一种更快的方法可以使 ggplot2 图形具有交互性。只需将 ggplot2 对象包装在
ggplotly()
中即可将其变成交互式图表,如下所示:
gg <- ggplot(
data = dba,
aes(x = forcats::fct_inorder(Strain),
y = genome_size_in_Mb,
fill = Subgroups)
) +
geom_bar(
position = "dodge",
stat = "identity",
width = 0.5
) +
scale_x_discrete(guide = guide_axis(angle = 90)) +
labs(title = "Genome Sizes Across Strains", x = "Strain", y = "Genome Size in Mb")
ggplotly(gg)
希望这有帮助!
library(plotly)
plot_ly(dba, x = ~forcats::fct_inorder(Strain),
y = ~genome_size_in_Mb,
color = ~Subgroups,
type = "bar") %>%
layout(title = list(text = "Genome Sizes Across Strains", x = 0.05),
xaxis = list(title = "Strain"),
yaxis = list(title = "Genome Size in Mb"),
legend = list(title = list(text = "Subgroups"), y = 0.5))
创建于 2024-07-08,使用 reprex v2.0.2