如何使用 ggplot2 在 R 中制作棒棒糖图?
棒棒糖图是线和点的组合。它显示数字和分类变量之间的关系,就像条形图一样。棒棒糖图可用于需要条形图和散点图的地方。与传统的条形图相比,这个单一的图可以帮助我们更好地可视化问题,并占用更少的墨水空间。
在 ggplot2 中,我们通过连接geom_segment()和geom_point()函数来制作棒棒糖图。
Syntax: ggplot(data, aes(x=x, y=y)) + geom_segment() +geom_point( )
创建基本的棒棒糖图
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value)) +
geom_point(size=4)
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3","Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with annotations
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value)) +
geom_point(size=4) +
geom_label(aes(name, value , label = signif(value)),
colour = "darkred", nudge_x = 0.35, size = 4)
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
"Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with custom colors
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value),
color="red", size=3) +
geom_point( color="green", size=10)
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
"Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with reordered data
ggplot(sample_data, aes(x=reorder(name,value),y=value)) +
geom_point(size = 3, colour = "black") +
geom_segment(aes(xend = name, yend = 0), size = 1.2)
输出:
为棒棒糖图添加注释
要将注释添加到 ggplot2 棒棒糖图中,我们使用 geom_label()函数:
Syntax: geom_label(aes(name, value, label = signif(value)), colour, nudge_x, size)
Here,
- color: determines the color of annotations
- nudge_x: determines the x shift of annotations
- size: determines the size of annotations
代码:
电阻
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3","Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with annotations
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value)) +
geom_point(size=4) +
geom_label(aes(name, value , label = signif(value)),
colour = "darkred", nudge_x = 0.35, size = 4)
输出:
自定义绘图
要自定义此图,我们可以使用 fill 属性更改线段和点的颜色。我们还可以使用 size 属性更改点的大小。为了向 geom_segment() 添加颜色,我们添加了一个具有所需颜色的颜色属性,并为线段的厚度添加了大小属性。为了给 geom_point() 添加颜色,我们添加了一个具有所需颜色的 color 属性,并添加了 size 属性以增加点的大小。
电阻
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
"Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with custom colors
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value),
color="red", size=3) +
geom_point( color="green", size=10)
输出:
重新排序棒棒糖图
我们可以使用 reorder()函数以 value 为基础对棒棒糖图重新排序。
Syntax: ggplot(sample_data, aes(x=reorder(name,value),y=value))
我们在美学参数中添加了重新排序函数,以使用值的基数按升序对我们的数据框进行重新排序。
电阻
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
"Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with reordered data
ggplot(sample_data, aes(x=reorder(name,value),y=value)) +
geom_point(size = 3, colour = "black") +
geom_segment(aes(xend = name, yend = 0), size = 1.2)
输出: