The following are the drawing syntaxes for 6 common types of scientific research graphs:
1. Scatter plot
Click here for a detailed explanation of the scatter plot<<
The most basic grammatical expression of ggplot drawing.
library(ggplot2)
x = seq.int(1,10,1)
y = seq.int(1,10,1)^2#Scatterplot
ggplot() + geom_point(aes(x,y)) +
labs(
title = "Scatterplot",
x = "x-axis"
)
2. Line chart
Click here for a detailed explanation of the line chart<<
Drawing board + line drawing + point drawing
animals = c("line", "elephant", "monkey")
numb = c(25,16,3)
ggplot() + geom_bar(aes(x=animals,y=numb),stat = "identity")
+
labs(
title = "条形图"
)
3. Bar chart
Click here for detailed explanation of bar chart<<
Among them, stat = "identity" means that it is a density (frequency) chart. In human terms, the Y-axis is the real value.
animals = c("lion", "elephant", "monkey")
numb = c(25,16,3)
ggplot() + geom_bar(aes(x=animals,y=numb), stat = "identity") +
labs(
title = "条形图"
)
4. Pie chart
Click here for a detailed explanation of the pie chart<<
Adding the new function coord_polar("y", start=0) on the basis of the bar chart is equivalent to converting the Y-axis of the circle into one circle of the circle. This part will be explained in detail in the following article.
require(scales)
df = data.frame(animals = c("lion", "monkey","elephant"),
numb = c(25,25,50))ggplot(df, aes(x="", y = numb, fill=animals)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank()
) +
geom_text(
aes(
y = numb/3 + c(0, cumsum(numb)[-length(numb)]),
label = percent(numb/sum(numb))),
size=5
)
5. Histogram
Click here for a detailed explanation of histogram<<
bins represents the number of columns, and binwidth controls the width of the columns;
color is the color of the border, and the internal color parameter is fill;
df = data.frame(num = c(1,1,3,5,9,2,5,5,6,8,7,9,7,5,4,8,5,4,7,8,9,5,4,1,2,0))
ggplot(df,aes(num)) + geom_histogram(bins = 8,color = "white")
6. Boxplot
Click here for a detailed explanation of the box plot<<
data("ToothGrowth") refers to a database;
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
ggplot(ToothGrowth, aes(x = dose, y = len)) +geom_boxplot()
Summarize:
This article mainly introduces the basic syntax of six common types of scientific research graphs. The following articles will explain the details of each graph in detail...