- 两个变量
qplot(x = age, y = friend_count, data = pf) #默认散点图
ggplot(aes(x = age, y = friend_count), data = pf) +
geom_point()
- 避免过度绘图:α
ggplot(aes(x = age, y = friend_count), data = pf) +
geom_point(α=1/20 ) #将取20个点等效一个黑点
- 添加一些抖动:geom_jitter
ggplot(aes(x = age, y = friend_count), data = pf) +
geom_jitter()
如年龄虽然是连续的 但我们只有整数岁数 所以使用抖动向每个年龄添加一些抖动
- 坐标轴变换:coord_trans
使用geom_point添加抖动的时候 需要注意——x轴从零开始(有些人的好友数为零) 因此,坐标轴平方根变换就产生虚数了 ,我们需要设置一下添加抖动的位置
library(ggplot2)
ggplot(aes(x=age,y=friendships_initiated),data = pf)+
geom_point(alpha = 1/20,position = position_jitter(h=0))+ #这里用的position_jitter和geom_jitter 等效,设置位置
xlim(13,90)+
coord_trans(y="sqrt") #y轴变为平方根
等效写法
library(ggplot2)
ggplot(aes(x=age,y=friendships_initiated),data = pf)+
geom_jitter(alpha = 1/20,position = position_jitter(h=0))+
xlim(13,90)+
coord_trans(y="sqrt")