R语言 使用ggplot2绘制线图

R语言 使用ggplot2绘制线图

在线形图中,我们有横轴值,通过横轴值,线形将被排序并使用纵轴值连接。我们将使用R包 ggplot2 ,其中有几个层次。

首先,如果之前没有在R Studio中安装ggplot2包,你需要安装它。

使用的函数

  • geom_line 按照水平(x)轴上的变量顺序连接它们。

语法

geom_line(mapping=NULL, data=NULL, stat="identity", position="identity", ...)

  • geom_path 以与data相同的顺序连接观察结果

语法

geom_path(mapping=NULL, data=NULL, stat="identity", position="identity",... )

单线图

在本节中,我们将处理一个单线图,还将讨论有助于其外观的各种属性。

使用中的数据集

# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
head(val)
R

输出

在R中使用ggplot2绘制线图

基本折线图

对于一个简单的折线图来说,数据被粗略地传递给函数,并带有一些必要的属性。

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Basic Line
ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line()+
  geom_point()
R

输出

在R中使用ggplot2绘制线图

格式化线

  • 线条类型

ggplot2提供了各种线条类型。例如:虚线、双破折号、虚线等等。这个属性的传递有一个必要的值。

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Format the line type
ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(linetype = "dotted")+
  geom_point()
R

输出

在R中使用ggplot2绘制线图

  • 线条颜色

使用命令 color ,所需颜色写在geom_line( )内的双引号[” “]中。

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Format the line color
ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(color="green")+
  geom_point()
R

输出

在R中使用ggplot2绘制线图

  • 线条大小

行的大小可以通过命令 size 来改变,并在geom_line( )中提供size的值。

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Format the line size
ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(color="green",size=1.5)+
  geom_point()
R

输出

在R中使用ggplot2绘制线图

添加图表标题、坐标轴标题

ggtitle() 带有适当的标题,可用于 ,以添加图表标题, 实验室 又带有适当的输入,可用于添加坐标轴标题。

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Adding titles
line<-ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(color="green",size=1.5)+
  geom_point()
 
line+ggtitle("Courses vs Students Enrolled in GeeksforGeeks")+
  labs(x="Courses",y="Number of Students")
R

输出

在R中使用ggplot2绘制线图

改变主题

使用theme_theme_name()来添加主题。在R库中有很多可用的主题。例如:黑暗、经典等等。可以根据需要提供数值。

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Adding titles
line<-ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(color="green",size=1.5)+
  geom_point()
 
line+ggtitle("Courses vs Students Enrolled in GeeksforGeeks")+
  labs(x="Courses",y="Number of Students")+
  theme_dark()
R

输出

在R中使用ggplot2绘制线图

添加箭头

为了在行中添加一个箭头,使用了 网格 库。然后使用 arrow( )来添加箭头。也可以改变箭头的参数,如角度、类型、末端。

例子

library(ggplot2)
library(grid)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Adding an arrow
ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(arrow=arrow())+
  geom_point()
 
# Adding  closed arrow on both ends of the line
arr=arrow(angle = 20, ends = "both", type = "closed")
ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(arrow=arr)+
  geom_point()
R

输出

在R中使用ggplot2绘制线图

添加数据标签

使用label来获取y轴上的数值,使用nudge_y来放置数据标签。

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Adding data label
ggplot(data=val, aes(x=course, y=num, group=1, label=num)) +
  geom_line()+
  geom_point()+
  geom_text(nudge_y = 2)
R

输出

在R中使用ggplot2绘制线图

缩放轴:

使用 xlim( ) 来改变x轴的比例,使用 ylim( ) 来改变y轴的比例,并将适当的值传递给这些。

语法

xlim(min,max)
ylim(min,max)

例子

library(ggplot2)
 
# Create data for chart
val <-data.frame(course=c('DSA','C++','R','Python'),
                num=c(77,55,80,60))
 
# Storing the line plot
ln <-ggplot(data=val, aes(x=course, y=num, group=1)) +
  geom_line(color="green",size=2)+
  geom_point()
 
# y-axis limits
ln+ylim(0,100)+
theme_dark()
R

输出

在R中使用ggplot2绘制线图

绘制多条线

对于将多个图绘制成一个图,除了必须将组属性设置为将绘制不同线条的列的名称外,没有任何变化。

例子

library(ggplot2)
 
# Inserting data
vacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2),
                  dose=rep(c("D1", "D2"),2),
                  slots=c(33, 45, 66, 50))
 
# Plotting line with multiple groups
ggplot(data=vacc, aes(x=dose, y=slots, group=type)) +
  geom_line(linetype="longdash", color="green", size=1.5)+
  geom_point(color="red", size=5)+
  theme_dark()
R

输出

在R中使用ggplot2绘制线图

你也可以在上面的折线图中添加标题、坐标轴标题、数据标签,如上节所述。

绘图的格式化:

  • 根据组别使用不同的线条类型

为了通过改变线的类型来区分线,在geom_line()中提供线的类型,在geom_point()中提供图例的形状。

例子

library(ggplot)
 
# Inserting data
vacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2),
                  dose=rep(c("D1", "D2"),2),
                  slots=c(33, 45, 66, 50))
 
# Changing the line type on the basis of groups
ggplot(vacc, aes(x=dose, y=slots, group=type)) +
  geom_line(aes(linetype=type))+
  geom_point()+
  theme_classic()
 
# Changing the line type on the basis of groups and also the shape of points
ggplot(vacc, aes(x=dose, y=slots, group=type)) +
  geom_line(aes(linetype=type))+
  geom_point(aes(shape=type))+
  theme_classic()
R

输出

在R中使用ggplot2绘制线图

  • 在组的基础上分配不同的线条颜色

下面的代码使用变量 “type “的级别自动控制颜色。它将为每一行分配单独的颜色。

例子

library(ggplot2)
 
# Inserting data
vacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2),
                  dose=rep(c("D1", "D2"),2),
                  slots=c(33, 45, 66, 50))
 
# Change line color by group type of vaccine
ln <-ggplot(vacc, aes(x=dose, y=slots, group=type)) +
  geom_line(aes(color=type))+
  geom_point(aes(color=type))+
  theme_classic()
ln
R

输出

在R中使用ggplot2绘制线图

要手动输入颜色,你可以使用:

  • scale_color_brewer( ) : 它使用RColorBrewer软件包中的不同调色板。它有各种颜色的调色板。
  • scale_color_manual( ) : 它用于手动添加离散的颜色。

例子

library(ggplot2)
 
# Inserting data
vacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2),
                  dose=rep(c("D1", "D2"),2),
                  slots=c(33, 45, 66, 50))
 
# Change line color by group type of vaccine
ln <-ggplot(vacc, aes(x=dose, y=slots, group=type)) +
  geom_line(aes(color=type))+
  geom_point(aes(color=type))+
  theme_classic()
 
# Adding line colors using brewer color palette
ln+scale_color_brewer(palette="Set2")
 
# Adding line colors using color manual
ln+scale_color_manual(values=c("green", "blue"))
R

输出

在R中使用ggplot2绘制线图

  • 改变图例的位置

为了改变图例的位置,主题函数的legen.position属性被传入所需的值。

语法

theme(legend.position=”pos”)

pos 可以是顶部、右侧、底部、左侧或无。

例子

library(ggplot2)
 
# Inserting data
vacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2),
                  dose=rep(c("D1", "D2"),2),
                  slots=c(33, 45, 66, 50))
 
# Change line color by group type of vaccine
ln <-ggplot(vacc, aes(x=dose, y=slots, group=type)) +
  geom_line(aes(color=type))+
  geom_point(aes(color=type))+
  theme_classic()
 
ln <- ln + scale_color_brewer(palette="Dark2")+
  theme_classic()
 
# Legend at top
ln + theme(legend.position="top")
 
# Legend at left
ln + theme(legend.position="left")
 
# Remove legend
ln + theme(legend.position="none")
R

输出

在R中使用ggplot2绘制线图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册