Preliminary study on Ggplot2 geom__ making area chart

Source: Internet
Author: User

we are probably not unfamiliar with the following infographic, which uses 100% stacked area charts to represent the proportions of different countries in different periods of time. This is a very interesting expression, and the area chart is a very common data graph, now let's look at how to use Ggplot2 and Plot.area in R to make an area chart.          
before making a chart, let's first create some random sample data . The R code is as follows:
  1. 1 # Create random numbers2Set.seed (3)
    3# Create a time series (0-20 of Time Step)4T.step<-seq (0,20)
    5 # Create 10 sets of variable names (from A to J)6Grps<-letters[1:10]
    7 # Create a time series of 10 sets of variables consisting of random numbers8Grp.dat<-runif (Length (t.step) *length (GRPs), 5,15)
    9 # Create the desired dataframe for the drawingTenGrp.dat<-matrix (Grp.dat,nrow=length (t.step), ncol=Length (GRPs)) OneGrp.dat<-data.frame (Grp.dat,row.names=t.step)
Data results:

Next, we use Plot.area to draw a simple area chart with the following code:
    1. 1 Source ("Https://gist.github.com/fawda123/6589541/raw/8de8b1f26c7904ad5b32d56ce0902e1d93b89420/plot_ AREA.R ")2 plot.area (Grp.dat)

The effect is as follows:
 
 for Plot.area, there are a lot of parameters can be adjusted to get different effects of the graph, such as color, coordinate name, whether 100% accumulation and so on. The specific parameter settings can be referenced as follows:
If we adjust the color:
    1. 1 plot.area (grp.dat,col=c (' Red ', ' lightgreen ', ' Purple '))
the results are as follows:
Let's take a look at how to draw an area chart with Ggplot2. Before drawing, first we need to convert our data format, we need to summarize multiple columns into a variable column (variable) and a numeric column (value), the code to transpose the data is as follows:
    1. 1 p.dat<-data.frame (Step=row.names (grp.dat), grp.dat,stringsasfactors=F)2 p.dat< -melt (p.dat,id= ' Step ')3 p.dat$step<-as.numeric (p.dat$step)
Next, we need to import the Ggplot2, as well as plot the area chart, with the following code:
    1.  Span style= "color: #008080;" >1  2  require (Ggplot2)  3  4  require (Gridextra)  6  # Plot Area chart  7  P<-ggplot (P.dat,aes (x=step,y= value))  8  p1<-p + geom_area (AES (fill=variable)) + Theme (legend.position= "Bottom" Span style= "color: #000000;" >)  9  p2<-p + geom_area (Aes (fill=variable), position= ' Fill ') 

       

 Let's take a look at the effects of P1 and P2 respectively:   
  of course, Geom has more fun apps that can be stamped: http://docs.ggplot2.org/current/geom_area.html   



From Wiznote



Preliminary study on Ggplot2 geom__ making area chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.