首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ggplot2中向平面图的分类y轴添加自定义标签和勾号。

在ggplot2中向平面图的分类y轴添加自定义标签和勾号。
EN

Stack Overflow用户
提问于 2021-03-02 21:16:01
回答 1查看 324关注 0票数 0

我想在下面显示的"No.14"条形图中添加另一行空瓷砖。该行将没有数据,因此仅向dataframe添加一个空行是不够的,因为它在图例中创建了一个我不想要的NA项。

以下是数据:

代码语言:javascript
运行
复制
> dput(coupler.graph)
structure(list(Category = c("HBC", "TC", "BSC", "GSC", "GSC", 
"SSC", "SSC", "GSC", "GSC", "SSC", "SSC", "SSC", "HBC", "TC", 
"BSC", "BSC", "GSC", "GSC", "SSC", "HBC", "HBC", "TC", "TC", 
"BSC", "GSC", "GSC", "GSC", "GSC", "GSC", "TC", "BSC", "BSC", 
"GSC", "GSC"), `Bar Size` = c("No. 5", "No. 5", "No. 5", "No. 5", 
"No. 5", "No. 6", "No. 6", "No. 6", "No. 6", "No. 8", "No. 8", 
"No. 8", "No. 8", "No. 8", "No. 8", "No. 8", "No. 8", "No. 8", 
"No. 10", "No. 10", "No. 10", "No. 10", "No. 10", "No. 10", "No. 10", 
"No. 10", "No. 10", "No. 11", "No. 11", "No. 18", "No. 18", "No. 18", 
"No. 18", "No. 18"), `No. Bars` = c(3, 9, 3, 6, 6, 85, 85, 7, 
7, 90, 90, 90, 7, 9, 6, 6, 21, 21, 9, 22, 22, 27, 27, 13, 25, 
25, 25, 8, 8, 4, 4, 4, 4, 4), Failure = c("Bar fracture", "Bar fracture", 
"Bar fracture", "Bar pullout", "Bar fracture", "Bar pullout", 
"Bar fracture", "Coupler failure", "Bar fracture", "Coupler failure", 
"Bar pullout", "Bar fracture", "Bar fracture", "Bar fracture", 
"Bar pullout", "Bar fracture", "Bar fracture", "Bar pullout", 
"Coupler failure", "Coupler failure", "Bar fracture", "Coupler failure", 
"Bar fracture", "Bar fracture", "Bar pullout", "Bar fracture", 
"Coupler failure", "Bar fracture", "Coupler failure", "Coupler failure", 
"Bar fracture", "Bar pullout", "Bar fracture", "Coupler failure"
), x = c("1-3", "7-9", "1-3", "5-7", "5-7", "30-90", "30-90", 
"5-7", "5-7", "30-90", "30-90", "30-90", "5-7", "7-9", "5-7", 
"5-7", "20-30", "20-30", "7-9", "20-30", "20-30", "20-30", "20-30", 
"11-15", "20-30", "20-30", "20-30", "7-9", "7-9", "3-5", "3-5", 
"3-5", "3-5", "3-5")), row.names = c(NA, -34L), class = c("tbl_df", 
"tbl", "data.frame"))

我创建交互式情节的代码:

labels1 <-因子(“0”、"1-3“、"3-5”、"5-7“、"7-9”、"9-11“、"11-15”、"15-20“、"20-90"),等级= c("0”、"1-3“、"3-5”、"5-7“、"7-9”、"9-11“、"11-15”、"15-20“、"20-30”),“30-90”) values1 <- c(“白色”、“#ffffd9 9”、"#edf8b1“、"#c7e9b4”、"#7fcdbb“、"#41b6c4”、"#1d91c0“、"#225ea8”、"#253494“、"#081d58") labels1 <- factor(c("0”、"1-3“、"3-5”、"5-7“、"7-9”、"9-11“、"11-15”、"15-20“),“20-90”,级别= c("0“、"1-3”、"3-5“、"5-7”、"7-9“、"9-11”、"11-15“、"15-20”、"20-30“、”20-30“、"30-90")) values1 <- c(”白色“、”#ffffd9 9“、"#edf8b1”、"#c7e9b4“、"#7fcdbb”、"#41b6c4“、"#1d91c0”、"#225ea8“),"#253494“、"#081d58")

代码语言:javascript
运行
复制
plot1 <- ggplot(coupler.graph) + aes(x = Category, y = fct_inorder(`Bar Size`), fill = factor(x, 
                                    levels = c("0", "1-3", "3-5", "5-7", "7-9",
                                               "9-11", "11-15",  "15-20","20-30", "30-90"))) +
 geom_tile(width=0.9, height=0.9) + theme_classic() + scale_fill_manual(labels = factor(labels1), values = values1) +
 labs(x = "Splicer Type", y = "Bar Size") +
 theme(plot.title = element_blank(), axis.text =  element_text(color = "black", size = 12), axis.ticks.x = element_blank(),
       axis.line = element_line(color = "black", size = 0.2), axis.ticks.y = element_line(color = "black", size = 0.2),
       axis.title.y = element_text(color = "black", size = 16, margin = margin(0,40,0,0)), 
       axis.title.x = element_text(color = "black", size = 16, margin = margin(35,0,0,0)),
       legend.title = element_blank(), legend.text = element_text(color = "black", size = 12)) 

ggplotly(
  p = ggplot2::last_plot(),
  width = NULL,
  height = NULL,
  tooltip = c("Category", "Failure"),
  dynamicTicks = FALSE,
  layerData = 1,
  originalData = TRUE,) %>% add_annotations( text="Number of\nSpecimens", xref="paper", yref="paper",
                  x=1.1, xanchor="left", y=0.9, yanchor="bottom", font = list(size = 18),
                  legendtitle=TRUE, showarrow=FALSE ) %>%
  layout(yaxis = list(title = list(text = "Bar Size", standoff = 30L)),
             xaxis = list(title = list(text = "Bar Size",standoff = 30L)),
             legend = list(orientation = "v", x = 1.1, y = 0.13))

由此产生的阴谋:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-02 22:00:55

你可以定义你的休息时间:

bar_list = c("No. 5", "No. 6", "No. 8", "No. 10", "No. 11", "No. 14", "No. 18")

然后

scale_y_discrete(limits = bar_list) +在你的ggplot中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66447263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档