In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "R language to visually beautify line charts, smooth curves and path diagrams". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
Today I will tell you about the path map, smooth curve and line chart and their beautification.
Here are three special layer functions for designing lines:
Geom_smooth (), geom_path (), geom_line ()
The following are explained separately:
About geom_smooth (): smooth Curv
Ggplot (mpg,aes (cty,hwy)) + geom_point () + geom_smooth ()
The smooth curve is specially used to fit the trend of the scatter chart, and there is a default fitting rule in the geom_smooth function (according to the fitting rules calculated by the statistical algorithm, not the actual points).
There is a default method parameter in parentheses, which represents the statistical algorithm used to fit the curve, and se represents the error range. That is, the color band around the fitting line.
The common types in method are lm\ glm\ gam\ loess\ rlm and so on. When the dataset record is less than 1000, the default parameter of method is loess, and when it is greater than 1000, it is gam.
There are only 234 records in the mpg dataset used in this example. The method parameter that should be used by default when using the geom_smooth parameter is loess. We can check it here:
Ggplot (mpg,aes (cty,hwy)) + geom_point () + geom_smooth (method= "loess")
The color band of the error range can be controlled by the se parameter. The default se=TRUE is set to se=FALSE.
Ggplot (mpg,aes (cty,hwy)) + geom_point () + geom_smooth (method= "loess", se=FALSE)
Next, let's try several other fitting curves in turn:
Ggplot (mpg,aes (cty,hwy)) + geom_point () + geom_smooth (method= "lm", se=FALSE)
Ggplot (mpg,aes (cty,hwy)) + geom_point () + geom_smooth (method= "glm", se=FALSE)
Ggplot (mpg,aes (cty,hwy)) + geom_point () + geom_smooth (method= "gam", se=FALSE)
It doesn't seem to make much difference, probably because the dataset is too small.
Path map: geom_path ()
The above function parameters can draw a path map, which is actually the path formed by linking all the points in turn according to the order in which each data point appears:
Ggplot (mpg,aes (cty,hwy)) + geom_path ()
At first glance, this kind of map may seem meaningless, but in some special situations, it can express very important information, such as migratory bird migration, virus transmission, and even aircraft routes.
Ggplot (mpg,aes (cty,hwy)) + geom_point () + geom_path ()
You can add a point function and a path function at the same time to create a path map with a little mark.
Ggplot (mpg,aes (cty,hwy)) + geom_point (colour= "steelblue") + geom_path (colour= "red")
The layer objects in each layer function can be defined individually.
Line chart: geom_line ()
The above is the layer function of the line chart; the only difference between it and the path map is that before connecting the points, it will be arranged in the overall ascending order of the x-axis data pairs, so the final connection is a very clear sequence of broken lines, there will be no intersection. (the path map is joined in the order in which the actual data points appear in the dataset.)
Ggplot (mpg,aes (cty,hwy)) + geom_line (colour= "steelblue")
Ggplot (mpg,aes (cty,hwy)) + geom_point (colour= "black") + geom_line (colour= "steelblue")
To see the difference more clearly, let's change a variable:
Ggplot (CO2,aes (uptake,conc)) + geom_line (colour= "steelblue") + geom_point (colour= "black")
Ggplot (iris,aes (Sepal.Length,Sepal.Width)) + geom_line (colour= "steelblue") + geom_point (colour= "white", shape=21,size=4,fill= "Tomato")
The line chart after adding the classification variable:
Ggplot (iris,aes (Sepal.Length,Sepal.Width,colour=Species)) + geom_line () + geom_point (colour= "white", shape=21,size=4,fill= "Tomato")
The default position parameter of the broken line should be position=identity, and we can also try to use several other parameters
Ggplot (iris,aes (Sepal.Length,Sepal.Width,colour=Species)) + geom_line (position= "identity") + geom_point (colour= "white", shape=21,size=4,fill= "Tomato")
Ggplot (iris,aes (Sepal.Length,Sepal.Width,colour=Species)) + geom_line (position= "stack") + geom_point (colour= "white", shape=21,size=4,fill= "Tomato", position= "stack")
Ggplot (iris,aes (Sepal.Length,Sepal.Width,colour=Species)) + geom_line () + facet_grid (. ~ Species) + geom_point (colour= "white", shape=21,size=4,fill= "Tomato") # facet group chart:
Ggplot (iris,aes (Sepal.Length,Sepal.Width,colour=Species)) + geom_line () + facet_grid (. ~ Species) + geom_point (colour= "white", shape=21,size=4,fill= "Tomato") + theme_wsj ("rgby", ") + scale_colour_wsj () + ggtitle (" line plot ") + guides (colour=guide_legend (title=NULL))
Ggplot (iris,aes (Sepal.Length,Sepal.Width,colour=Species)) + geom_line () + facet_grid (. ~ Species) + geom_point (colour= "white", shape=21,size=4,fill= "Tomato") + theme_economist () + scale_colour_economist () + ggtitle ("line plot") + guides (colour=guide_legend (title=NULL))
This is the end of the content of "R language to visually beautify line chart, smooth curve and path map". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.