Fourth: R language Data visualization line chart, stacked map, stacked area chart

Source: Internet
Author: User
Tags border color ggplot

Preface

A line chart is typically used to visualize the dependencies of two consecutive variables , where the horizontal axis is often the timeline.

But the horizontal axis is not necessarily a continuous variable, it can be ordered discrete variable.

draw a basic line chart

This example uses the following test data set:

The drawing method is to first call the Ggplot function to select the dataset and indicate the horizontal axis in the AES parameter. Then call the bar graph function geom_line () to draw a basic line chart. The example code for the R language is as follows:

# base function Ggplot (BOD, AES (x = time, y = demand)) +  # line graph function  geom_line ()

 Operation Result:

add a data marker to a line chart

This example uses the following test data set:

If you want to mark each sample point in the dataset in a line chart, simply add a scatter map layer (Geom_line ()) to the original base. The R language implementation code is as follows:

# base function Ggplot (BOD, AES (x = time, y = demand)) +  # line graph function  geom_line () +  # Scatter graph function  geom_point ()

Operation Result:

If you are dissatisfied with the style of the marker, you can adjust it by modifying the parameters of the Geom_point (). If you can customize the tag to a pink box, the R language implementation code is as follows:

# base function Ggplot (BOD, AES (x = time, y = demand)) +  # line graph function  geom_line () +  # Scatter chart function: Size setting, shape setting, colour setting border color, f Ill set fill color  geom_point (size = 4, shape = $, colour = "darkred", fill = "Pink")

Operation Result:

You can also customize the markup as a white round box, and the R language implementation code is as follows:

# base function Ggplot (BOD, AES (x = time, y = demand)) +  # line graph function  geom_line () +  # Scatter chart function: Size setting the shape, fill setting fill color 
   geom_point (size = 4, shape = 21,fill = "White")

Operating effect:

For more customization methods, refer to the description of Geom_point () in the function manual.

draw a multi-line chart

This example uses the following test data set:

The method of drawing is to set an aesthetic variable in the set of aesthetic parameters of the base function on the base line chart. You can specify colour or Linetype two parameters, respectively, different groups are expressed in different color/line lines. The example code for the R language is as follows:

# Base function: Colour set group Ggplot (TG, aes (x = dose, y = length, colour = Supp)) +  # line graph function  geom_line ()

Operation Result:

If data marker processing is done in a multi-line chart, the markers for different polylines may be duplicated. The Position_dodge parameter bias processing can be set in the drawing function. The R language implementation code is as follows:

# Base function: Colour set group Ggplot (TG, aes (x = dose, y = length, shape = supp)) +  # line graph function: Position set offset  geom_line (position = P Osition_dodge (. 2)) +  # Scatter plot function: Position set offset item  geom_point (position = Position_dodge (. 2), size = 4)

Operation Result:

Modify Line Style

This example uses the following test data set:

The drawing method is simple, and the Linetype parameter can be set in the Geom_line () function. The example code for the R language is as follows:

# base function Ggplot (BOD, AES (x = time, y = demand)) +  # line graph function: Linetype set linear  geom_line (Linetype = "dashed", size = 1, colour = "Orange")

Operating effect:

Line chart Confidence field

This example uses the following test data set:

The drawing method is called the Geom_ribbon function of Ggplot2, which can draw a "band". We can specify the upper and lower bounds of the band as confidence intervals, thus visualizing the confidence field of the line chart. The test data set unc10y represents the confidence interval under the anomaly10y 95% confidence level.

The example code for the R language is as follows:

# base function Ggplot (Clim, AES (x = year, anomaly10y)) +  # Strip graph function: ymin set the lower bound, ymax set upper bound;  Geom_ribbon (aes (ymin = anomaly10y-unc10y, ymax=anomaly10y+unc10y), alpha = 0.2) +  # line graph function  geom_line ()

Operating effect:

draw a basic area chart

This example uses the following test data set:

The drawing method is to set the data set and the vertical axis in the base function, and then add the Geom_area () function to draw an area chart. The example code for the R language is as follows:

# base function Ggplot (Sunspotyear, AES (x = year, y = sunspots)) +  #  area chart function: Fill sets the fill color, alpha sets the transparency;  geom_area (fill = " Blue ", alpha =. 2)

Operating effect:

plot a stacked area chart

This example uses the following test data set:

The drawing method is to set the fill parameter on the basic area graph toward the aesthetic feature set of the base function. The example code for the R language is as follows:

# Base function: Fill set GROUP by Ggplot (Uspopage, AES (x = year, y = Thousands, fill = agegroup)) +  # area graph function  Geom_area ()

Operating effect:

Obviously, each packet data has a size relationship. As a result, you can set the color palette to fade color. The R language implementation code looks like this:

# base function Ggplot (Uspopage, AES (x = year, y = Thousands, fill = agegroup)) +  # area graph function  Geom_area () +  # Palette ruler: Breaks invert legend Order  Scale_fill_brewer (palette = "Blues", breaks = Rev (levels (Uspopage$agegroup)))

Operation Result:

If you need to draw a percentage stacked chart, simply modify the underlying data based on the work above.

The R language implementation code is as follows:

# Convert data to percent format Uspopage_prop = ddply (Uspopage, "year", transform, Percent = Thousands/sum (Thousands) * 100) # base function Ggplot (USP Opage_prop, AES (x = year, y = Percent, fill = agegroup)) +  # area graph function  Geom_area () +  # palette ruler  Scale_fill_brewer ( palette = "Blues", breaks = Rev (levels (Uspopage_prop$agegroup)))

Operation Result:

PS: For some users, you might want a legend with a darker color below. This requirement simply modifies the sort order of the examples in the aesthetic feature set of the base function.

PPS: The original data for a stacked area chart is usually a wide data format that needs to be implemented to convert it to a Long data format. The conversion method can refer to the second article in this series.

Fourth: R language Data visualization line chart, stacked map, stacked 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.