代码拉取完成,页面将自动刷新
同步操作将从 程序员维C社区/graph 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google Analytics + OutboundLink + Google Adsense -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-79254642-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-79254642-1');
</script>
<script>
var getOutboundLink = function(url) {
gtag('event', 'click', {
'event_category': 'outbound',
'event_label': url,
'transport_type': 'beacon',
'event_callback': function(){document.location = url;}
});
}
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-3523953066677938",
enable_page_level_ads: true
});
</script>
<!-- Google Analytics + OutboundLink + Google Adsense -->
<meta charset="utf-8">
<title>Background manipulation with R and ggplot2 – the R Graph Gallery</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="This post describes all the available options to customize chart background with R and ggplot2. It shows how to control the background color and the minor and major gridlines.">
<meta name="keywords" content="R,ggplot2,tidyverse,Example,Data,Dataviz,Datavisualization,Plot,Chart,Graph,Learning,Caveat,Pitfall,Mistake">
<meta name="author" content="Yan Holtz">
<link rel="icon" href="img/logo/R_single_small.png">
<!-- Control appearance when shared by social media -->
<meta property="og:title" content="Background manipulation with R and ggplot2" />
<meta property="og:image" content="img/overview_RGG.png" />
<meta property="og:description" content="This post describes all the available options to customize chart background with R and ggplot2. It shows how to control the background color and the minor and major gridlines." />
<meta property='og:url' content="https://www.r-graph-gallery.com/240-custom-layout-background-ggplot2.html" />
<meta property="og:type" content="website" />
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom fonts for this template -->
<link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template -->
<link href="css/agency.css" rel="stylesheet">
<!-- JQUERY -->
<script src="vendor/jquery/jquery.min.js"></script>
</head>
<body data-spy="scroll" data-target="#myScrollspy" data-offset="1">
<!-- THIS ALLOWS TO INSERT THE MENU THAT IS STORED IN A MENU.HTML FILE-->
<nav class="navbar navbar-expand-lg fixed-top" id="mainNav"></nav>
<script>
$(function(){
$("#mainNav").load("html_chunk/menu.html");
});
</script>
<!-- THIS ALLOWS TO INSERT THE MODAL OF THE MENU THAT IS STORED IN A MENU_MODAL.HTML FILE-->
<div id="modal_menu_insertion"> </div>
<script>
$(function(){
$("#modal_menu_insertion").load("html_chunk/menu_modal.html");
});
</script>
<!-- Header = Title in big + social media Icon + quick description -->
<header class="masthead" style="padding-bottom: 30px;">
<div class="textlanding">
<center><h1>Background manipulation with R and ggplot2</h1></center>
<hr class="short_hr">
<br>
<ul class="list-inline social-buttons">
<li class="list-inline-item">
<a href="https://twitter.com/R_Graph_Gallery">
<i class="fa fa-twitter"></i>
</a>
</li>
<li class="list-inline-item social-buttons">
<a href="https://github.com/holtzy">
<i class="fa fa-github" style="color: white"></i>
</a>
</li>
<li class="list-inline-item social-buttons">
<a href="https://www.linkedin.com/in/yan-holtz-2477534a/">
<i class="fa fa-linkedin"></i>
</a>
</li>
<li class="list-inline-item social-buttons">
<a href="https://www.yan-holtz.com">
<i class="fa fa-home"></i>
</a>
</li>
</ul>
<center><p style="max-width: 600px; margin-top: 40px">This post describes all the available options to customize chart background with <code>R</code> and <code>ggplot2</code>. It shows how to control the background color and the minor and major gridlines.</p></center>
<div style="text-align:center">
<a class="btn btn-secondary btn-xl text-uppercase js-scroll-trigger" href='ggplot2-package.html'>ggplot2 section</a>
<a class="btn btn-secondary btn-xl text-uppercase js-scroll-trigger" href='https://www.data-to-viz.com'>Data to Viz</a>
</div>
</div>
</header>
<!-- THIS ALLOWS TO INSERT THE ADVERTISEMENT BANNER THAT IS STORED IN A BANNER.HTML FILE-->
<div id="position_for_images"></div>
<script>
$(function(){
$("#position_for_images").load("html_chunk/images.html");
});
</script>
<!-- STYLE for chart pages but not the rest of tthe website -->
<style>
img {
margin-top: 20px;
}
</style>
<div class="container">
<h1 id="default-ggplot2-background">Default <code>ggplot2</code> background</h1>
<hr />
<div class="row">
<div class="col-md-6 col-sm-12 align-self-center">
<p>Let’s start with a very basic <code>ggplot2</code> <a href="scatterplot.html">scatterplot</a>. By default, <code>ggplot2</code> offers a grey background with white major and minor gridlines.</p>
<p>It is possible to change that thanks to the <code>theme()</code> function. Keep reading to learn how!</p>
</div>
<div class="col-md-6 col-sm-12">
<p><img src="240-custom-layout-background-ggplot2_files/figure-html/thecode-1.png" width="100%" /></p>
</div>
</div>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co"># Load ggplot2</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="kw">library</span>(ggplot2)</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co"># Very basic chart</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">basic <-<span class="st"> </span><span class="kw">ggplot</span>( mtcars , <span class="kw">aes</span>(<span class="dt">x=</span>mpg, <span class="dt">y=</span>wt)) <span class="op">+</span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6"><span class="st"> </span><span class="kw">geom_point</span>()</a>
<a class="sourceLine" id="cb1-7" data-line-number="7">basic</a></code></pre></div>
<h1 id="background-color-plot.background-and-panel.background">Background color: <code>plot.background</code> and <code>panel.background</code></h1>
<hr />
<div class="row">
<div class="col-md-6 col-sm-12 align-self-center">
<p>Two options of the <code>theme()</code> functions are available to control the map background color. <code>plot_background</code> controls the color of the whole chart. <code>panel.background</code> controls the part between the axis.</p>
<p>Both are rectangles, their features is thus specified through an <code>element_rect()</code> function.</p>
</div>
<div class="col-md-6 col-sm-12">
<p><img src="240-custom-layout-background-ggplot2_files/figure-html/thecode2-1.png" width="100%" /></p>
</div>
</div>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1">basic <span class="op">+</span><span class="st"> </span><span class="kw">theme</span>(</a>
<a class="sourceLine" id="cb2-2" data-line-number="2"> <span class="dt">plot.background =</span> <span class="kw">element_rect</span>(<span class="dt">fill =</span> <span class="st">"green"</span>), </a>
<a class="sourceLine" id="cb2-3" data-line-number="3"> <span class="dt">panel.background =</span> <span class="kw">element_rect</span>(<span class="dt">fill =</span> <span class="st">"red"</span>, <span class="dt">colour=</span><span class="st">"blue"</span>)</a>
<a class="sourceLine" id="cb2-4" data-line-number="4"> )</a></code></pre></div>
<h1 id="customize-the-grid-panel.grid.major-and-panel.grid.minor">Customize the grid: <code>panel.grid.major</code> and <code>panel.grid.minor</code></h1>
<hr />
<div class="row">
<div class="col-md-6 col-sm-12">
<p><img src="240-custom-layout-background-ggplot2_files/figure-html/thecode3a-1.png" width="80%" style="display: block; margin: auto;" /></p>
</div>
<div class="col-md-6 col-sm-12">
<p><img src="240-custom-layout-background-ggplot2_files/figure-html/thecode3b-1.png" width="80%" style="display: block; margin: auto;" /></p>
</div>
</div>
<p>Two main types of grid exist with <code>ggplot2</code>: major and minor. They are controled thanks to the <code>panel.grid.major</code> and <code>panel.grid.minor</code> options.</p>
<p>Once more, you can add the options <code>.y</code> or <code>.x</code> at the end of the function name to control one orientation only.</p>
<p>Features are wrapped in an <code>element_line()</code> function. Specifying <code>element_blanck()</code> will simply removing the grid.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="co"># Modify for both axis</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">basic <span class="op">+</span><span class="st"> </span><span class="kw">theme</span>(</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"> <span class="dt">panel.grid.major =</span> <span class="kw">element_line</span>(<span class="dt">colour =</span> <span class="st">"red"</span>, <span class="dt">linetype =</span> <span class="st">"dotted"</span>),</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"> <span class="dt">panel.grid.minor =</span> <span class="kw">element_line</span>(<span class="dt">colour =</span> <span class="st">"blue"</span>, <span class="dt">size =</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb3-5" data-line-number="5"> )</a></code></pre></div>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="co"># Modify y axis only (remove)</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">basic <span class="op">+</span><span class="st"> </span><span class="kw">theme</span>(</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"> <span class="dt">panel.grid.major.y =</span> <span class="kw">element_blank</span>(),</a>
<a class="sourceLine" id="cb4-4" data-line-number="4"> <span class="dt">panel.grid.minor.y =</span> <span class="kw">element_blank</span>()</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">)</a></code></pre></div>
<!-- Close container -->
</div>
<!-- ============================ RELATED SECTION ============================ -->
<section class="bg-light" id="portfolio_landing" style="padding-top: 30px; padding-bottom: 30px; margin-top: 100px;">
<div class="container">
<p class="mySeryTitle">Related chart types</p>
<hr>
<div class="row">
<div class="col-md-2 col-sm-4 portfolio-item">
<a class="portfolio-link" href="stacked-barplot.html">
<div class="portfolio-hover">
<div class="portfolio-hover-content">
<i class="fa fa-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src="img/section/GroupedRed150.png" alt="">
</a>
<div class="captionPortfolio">Grouped and Stacked barplot</div>
</div>
<div class="col-md-2 col-sm-4 portfolio-item">
<a class="portfolio-link" href="treemap.html">
<div class="portfolio-hover">
<div class="portfolio-hover-content">
<i class="fa fa-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src="img/section/Tree150.png" alt="">
</a>
<div class="captionPortfolio">Treemap</div>
</div>
<div class="col-md-2 col-sm-4 portfolio-item">
<a class="portfolio-link" href="doughnut-plot.html">
<div class="portfolio-hover">
<div class="portfolio-hover-content">
<i class="fa fa-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src="img/section/Doughnut150.png" alt="">
</a>
<div class="captionPortfolio">Doughnut</div>
</div>
<div class="col-md-2 col-sm-4 portfolio-item">
<a class="portfolio-link" href="pie-plot.html">
<div class="portfolio-hover">
<div class="portfolio-hover-content">
<i class="fa fa-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src="img/section/Pie150.png" alt="">
</a>
<div class="captionPortfolio">Pie chart</div>
</div>
<div class="col-md-2 col-sm-4 portfolio-item">
<a class="portfolio-link" href="dendrogram.html">
<div class="portfolio-hover">
<div class="portfolio-hover-content">
<i class="fa fa-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src="img/section/Dendrogram150.png" alt="">
</a>
<div class="captionPortfolio">Dendrogram</div>
</div>
<div class="col-md-2 col-sm-4 portfolio-item">
<a class="portfolio-link" href="circle-packing.html">
<div class="portfolio-hover">
<div class="portfolio-hover-content">
<i class="fa fa-plus fa-3x"></i>
</div>
</div>
<img class="img-fluid" src="img/section/CircularPacking150.png" alt="">
</a>
<div class="captionPortfolio">Circular packing</div>
</div>
</div>
</div>
</section>
<!-- ============================ CONTACT SECTION ============================ -->
<section id="contact" class="bg" style="background-color: white; padding-top: 60px">
<div class="container">
<div class="row">
<div class="col-lg-2 text-center"></div>
<div class="col-lg-8 text-center">
<br><br><br>
<h2 class="section-heading text-uppercase" style="color: black">Contact</h2>
<p>This document is a work by <a href="https://www.yan-holtz.com">Yan Holtz</a>. Any feedback is highly encouraged. You can fill an issue on <a href="https://github.com/holtzy/D3-graph-gallery/issues">Github</a>, drop me a message on <a href="https://twitter.com/R_Graph_Gallery">Twitter</a>, or send an email pasting <a href="">yan.holtz.data</a> with <a href="">gmail.com</a>.</p>
<div style="text-align:center">
<a class="btn btn-primary btn-xl text-uppercase js-scroll-trigger" href="https://github.com/holtzy">Github</a>
<a class="btn btn-primary btn-xl text-uppercase js-scroll-trigger" href="https://twitter.com/R_Graph_Gallery">Twitter</a>
</div>
</div>
</div>
</div>
</section>
<!-- ============================ FOOTER SECTION ============================ -->
<footer class="bg-light" id="myFooter">
<div class="container" >
<div class="row">
<div class="col-md-4">
<span class="copyright">Copyright © the R graph gallery 2018</span>
</div>
<div class="col-md-4">
<ul class="list-inline social-buttons">
<li class="list-inline-item">
<a href="https://twitter.com/R_Graph_Gallery">
<i class="fa fa-twitter"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://github.com/holtzy">
<i class="fa fa-github"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://www.linkedin.com/in/yan-holtz-2477534a/">
<i class="fa fa-linkedin"></i>
</a>
</li>
</ul>
</div>
<div class="col-md-4">
<ul class="list-inline quicklinks">
<li class="list-inline-item">
<a href="#">Privacy Policy</a>
</li>
<li class="list-inline-item">
<a href="#">Terms of Use</a>
</li>
</ul>
</div>
</div>
</div>
</footer>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- ============================ JAVASCRIPT SECTION ============================ -->
<!-- Bootstrap core JavaScript -->
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Custom scripts for this template -->
<script src="js/agency.min.js"></script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。