r-使用geom_line分组时在ggplot2中添加手动标签

时间:2019-01-03 13:55:23

标签: r ggplot2

使用ggplot2包的多种功能来制作geom_line的图,当将其中的几个合并时,会产生奇怪的输出,如下所示:

必要的库:

library(ggplot2)
library(dplyr)
library(plotly)

样本数据集:

df <- data.frame(a = sample(LETTERS, 10, replace = FALSE), 
                 b = rnorm(10, mean = 2, sd = 5),
                 c = rnorm(10, mean = 15, sd = 5),
                 d = sample(letters, 10, replace = FALSE))

剧情:

ggplot(df, aes(x = a)) + 
  geom_line(aes(y = b, group = 1, color = "line_one"), size = 2, alpha = 0.6) +
  geom_line(aes(y = c, group = 1, color = "line_two"), size = 2, alpha = 0.6) +
  scale_y_continuous(sec.axis = sec_axis(~. + 10)) +
  labs(x = "My x axis",
   y = "My y axis") +
  theme(axis.text = element_text(angle = 90, hjust = 0.4, vjust = -0.5)) +
  geom_hline(yintercept = df$b %>% quantile(.99), 
             size = 2, 
             color = "tomato", 
             linetype = "dashed",
             alpha = 0.6) +
  scale_color_manual(
    name = "", 
    values = c("line_one" = "red", "line_two" = "blue")
  ) + 
  theme_light()

产生的输出未显示图例框中线条的颜色:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用.portfolio-box { position: relative; display: block; max-width: 650px; margin: 0 auto; } /**** Portfolio GIF Rollover States ****/ .project-rollover1 { background-image: url("../img/portfolio/thumbnails/01-reel.gif"); background-size: cover; } .project-rollover2 { background-image: url("../img/portfolio/thumbnails/02-jg-wentworth.gif"); background-size: cover; } .portfolio-box .portfolio-box-caption { position: absolute; bottom: 0; display: block; width: 100%; height: 100%; text-align: center; opacity: 0; color: #fff; -webkit-transition: all 0.25s; transition: all 0.25s; } .portfolio-box:hover .portfolio-box-caption { opacity: 1; } .portfolio-modal { padding-right: 0px !important; } .portfolio-modal .modal-dialog { margin: 1rem; max-width: 100vw; } .portfolio-modal .modal-content { padding: 100px 0; background-image: url("../img/patterns/debut_light.png"); } .portfolio-modal .modal-content h2 { font-size: 2.5em; font-weight: bold; margin-bottom: 15px; color: #333333; } .portfolio-modal .modal-content p { margin: 10px 0 20px; font-size: 16px; } .portfolio-modal .modal-content p.item-intro { font-size: 16px; font-style: italic; margin: 10px 0 20px; font-family: 'Open Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; } .portfolio-modal .modal-content ul.list-inline { margin-top: 0; margin-bottom: 30px; } .portfolio-modal .modal-content img { margin-bottom: 30px; } .portfolio-modal .modal-content button { cursor: pointer; } .portfolio-modal .close-modal { position: absolute; top: 25px; right: 25px; width: 75px; height: 75px; cursor: pointer; background-color: transparent; } .portfolio-modal .close-modal:hover { opacity: 0.3; } .portfolio-modal .close-modal .lr { /* Safari and Chrome */ z-index: 1051; width: 1px; height: 75px; margin-left: 35px; /* IE 9 */ -webkit-transform: rotate(45deg); transform: rotate(45deg); background-color: #212529; } .portfolio-modal .close-modal .lr .rl { /* Safari and Chrome */ z-index: 1052; width: 1px; height: 75px; /* IE 9 */ -webkit-transform: rotate(90deg); transform: rotate(90deg); background-color: #212529; }时应始终使用长数据。这样,您无需将每一行都添加为一层,也不需要手动调整图例等。这里是基于您的示例(我使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/> HTML <!-- Portfolio Grid --> <section class="p-0 bg-light" id="portfolio"> <div class="container-fluid p-0"> <div class="row no-gutters"> <!-- Project 1 --> <div class="col-lg-4 col-sm-6"> <a class="portfolio-box" data-toggle="modal" href="#portfolioModal1"> Project1 <div class="portfolio-box-caption project-rollover1"> </div> </a> </div> <!-- Project 2 --> <div class="col-lg-4 col-sm-6"> <a class="portfolio-box" data-toggle="modal" href="#portfolioModal2"> Project2 <div class="portfolio-box-caption project-rollover2"> </div> </a> </div> </div> </div> </section> <!-- Portfolio Modals --> <!-- Modal 1 --> <div class="portfolio-modal modal fade" id="portfolioModal1" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="close-modal" data-dismiss="modal"> <div class="lr"> <div class="rl"></div> </div> </div> <div class="container"> <div class="row"> <div class="col-lg-8 mx-auto"> <div class="modal-body"> <!-- Project Details Go Here --> <h2 class="text-uppercase">2018 Reel</h2> <div class="container"> <div class="row"> <div class="embed-responsive embed-responsive-16by9"> <iframe id="nofocusvideo" class="embed-responsive-item" src="https://player.vimeo.com/video/250633785" allowfullscreen></iframe> </div> </div> </div> <p>Use this area to describe your project. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est blanditiis dolorem culpa incidunt minus dignissimos deserunt repellat aperiam quasi sunt officia expedita beatae cupiditate, maiores repudiandae, nostrum, reiciendis facere nemo!</p> <ul class="list-inline"> <li>Date: Mid 2018</li> <li>Client: Austin Bonelli</li> <li>Category: 2D Animation</li> </ul> <button class="btn btn-primary-pink" data-dismiss="modal" type="button"> <i class="fas fa-times"></i> Close Project </button> </div> </div> </div> </div> </div> </div> </div> <!-- Modal 2 --> <div class="portfolio-modal modal fade" id="portfolioModal2" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="close-modal" data-dismiss="modal"> <div class="lr"> <div class="rl"></div> </div> </div> <div class="container"> <div class="row"> <div class="col-lg-8 mx-auto"> <div class="modal-body"> <!-- Project Details Go Here --> <h2 class="text-uppercase">JG Wentworth</h2> <div class="container"> <div class="row"> <div class="embed-responsive embed-responsive-16by9"> <iframe id="nofocusvideo" class="embed-responsive-item" src="https://player.vimeo.com/video/202987459" allowfullscreen></iframe> </div> </div> </div> <p>Use this area to describe your project. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est blanditiis dolorem culpa incidunt minus dignissimos deserunt repellat aperiam quasi sunt officia expedita beatae cupiditate, maiores repudiandae, nostrum, reiciendis facere nemo!</p> <ul class="list-inline"> <li>Date: Mid 2018</li> <li>Client: Austin Bonelli</li> <li>Category: 2D Animation</li> </ul> <button class="btn btn-primary-pink" data-dismiss="modal" type="button"> <i class="fas fa-times"></i> Close Project </button> </div> </div> </div> </div> </div> </div> </div>转换为long ):

ggplot2

编辑:

另一个示例展示了具有不同层的长数据的灵活性:

tidyr::gather()