ggplot:调整y轴标签:放在两行上

时间:2018-08-23 10:32:44

标签: r ggplot2

在ggplot中,我想在两行上标记y轴,因为在1行中它是重叠的。

代码如下:

myd<- data.frame( var1 = rep(c("Newly infected","Mortality","TDR level"),each=3), 
                     samp = rep(c("Scen1: blablabla blabla blabla bla","Scen2","Scen3"),3), 
                  V3 = c(3.5,2,NA,8,2,NA,4,5,NA)/1.5, V2 = c(3.5,2,NA,8,3,NA,4,4.3,NA), V1 = c(1.5,0.2,5,5,3,0.2,4,-5,2) ) 

# rshaping data to long form for ggplot2 
library(reshape2)
meltd<- melt(myd, id.vars=1:2) 
#meltd<- meltd[-which(is.na(meltd$value)),]

ggplot(meltd, aes(x = var1, y = value, fill = variable, label = paste0(round(value * 100, 1), "%"))) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.6), width = 0.5) +
  facet_grid(samp ~ ., switch = "y", scales = "free_y", space = "free") +
  coord_flip() +
  scale_fill_manual("legend",values = c("V3" = "orange", "V2" = "red", "V1" = "blue", "Baseline" = "black")) +
  geom_text(aes(y = value + 0.4 * sign(value)), position = position_dodge(width = 0.6)) +
  theme_bw() +
  theme(
    legend.position = "none",
    strip.placement = "outside",
    #axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y = element_text(colour="black"),
    strip.text.y = element_text(size = 12, colour = "black"))+
  ylab("Relative change (in %)")

以下是输出:

Blabla too long on the y-axis label

编辑:通过在facet_grid()中添加labeller = label_wrap_gen(width = 6,multi_line = T),将其放在多行上。问题是我只需要2行。参见下图: Blabla on more than 2 lines

2 个答案:

答案 0 :(得分:2)

在facet_grid调用中,使用类似labeller=label_wrap_gen(width=6, multi_line=T)

答案 1 :(得分:1)

您可以像这样在名称中添加换行符:

.example-container {
  display: flex;
  flex-direction: column;
}

.login {
    margin-top: 80px;
    height: 450px;
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
    background-color: #274c7c
}
.material-icons {  
    margin-left: 2%;
    margin-bottom: 10px;
}
.otp-btn {
    margin: 30px 30px 30px 30px;
    margin-left: 10%;
    width: 80%;
    font-size:30px; 
    height: 55px;
    color:white;
    border-radius: 10px;
    background-color: #f6b319;
}

:host {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
}
img {
    height: 16%;
    width: 174px;
    margin-left: 15%;
    margin-right: auto;
    padding: 10px;
}
div.input {
    position: relative;
}
.form {
    display: block; 
    position: relative;
    flex: auto;
    min-width: 0;
    width: 245px !important;
    margin-left:15px;
}
.inputField
{
    width: 90%;
    font-size: 20px;

}
div.input label {
    position: absolute;
    top: 0;
    transform: translateY(-50%);
    left: 10px;
    background: white;
    padding: 5px 2px;
    color: white;
}

div.input input {
    padding: 10px 20px;
    font-size: 25px;
    outline: 0;
    color: white;
}

div.input {
    margin-top: 20px;
    color: white;
}


.example-container > * {
  width: 100%;
}

这将导致如下图所示:enter image description here

您可以在名称中特定数量的字符后自动输入myd<- data.frame( var1 = rep(c("Newly infected","Mortality","TDR level"),each=3), samp = rep(c("Scen1: blablabla blabla \n blabla bla","Scen2","Scen3"),3), V3 = c(3.5,2,NA,8,2,NA,4,5,NA)/1.5, V2 = c(3.5,2,NA,8,3,NA,4,4.3,NA), V1 = c(1.5,0.2,5,5,3,0.2,4,-5,2) ) ,如下所示:

\n