R中ggplot2散点图的控制点边框厚度
在本文中,我们将看到如何在 R 编程语言中控制 ggplot ScatterPlot 的点边界厚度。为此,我们将使用geom_point()函数。以下是关于 ggplot函数geom_point() 的简要信息。
Syntax : geom_point(size, color, fill, shape, stroke)
Parameter :
- size : Size of Points
- color : Color of Points/Border
- fill : Color of Points
- shape : Shape of Points in in range from 0 to 25
- stroke : Thickness of point border
Return : It creates scatterplots.
让我们首先创建一个没有修改的散点图,以便差异显而易见。
例子:
R
# Load package
library("ggplot2")
# Create data for plotting
data <- data.frame(x = rnorm(10), y = rnorm(10))
# Create Scatterplot with size 10 and green color
ggplot(data, aes(x, y)) +
geom_point(size = 10, color = "green")
R
# Load package
library("ggplot2")
# Create data for plotting
data <- data.frame(x = rnorm(10), y = rnorm(10))
# Create a border around points
# in Scatterplot
ggplot(data, aes(x, y)) +
geom_point(size = 10, color = "black",
fill = "green", shape = 21)
R
# Load package
library("ggplot2")
# Create data for plotting
data <- data.frame(x = rnorm(10), y = rnorm(10))
# Change thickness of the border
ggplot(data, aes(x, y)) +
geom_point(size = 10, color = "black",
fill = "green", shape = 21,
stroke = 4)
输出:
为了改变散点图中点的形状,即在散点图中的点周围做边框,我们使用 geom_point()函数的shape参数将值设置为21 ,它指定范围从 0 到 25 的点的形状。下图表示具有范围的点的形状。除此之外,我们使用填充参数来填充点的颜色和边框颜色的颜色参数。
例子:
电阻
# Load package
library("ggplot2")
# Create data for plotting
data <- data.frame(x = rnorm(10), y = rnorm(10))
# Create a border around points
# in Scatterplot
ggplot(data, aes(x, y)) +
geom_point(size = 10, color = "black",
fill = "green", shape = 21)
输出:
现在要更改边框的粗细,我们只需在 geom_point()函数使用一个名为stroke的参数,它指定 Scatterplot 中点的边框的粗细。
例子:
电阻
# Load package
library("ggplot2")
# Create data for plotting
data <- data.frame(x = rnorm(10), y = rnorm(10))
# Change thickness of the border
ggplot(data, aes(x, y)) +
geom_point(size = 10, color = "black",
fill = "green", shape = 21,
stroke = 4)
输出: