修改圖例legend位置
p + theme(legend.position="top") p + theme(legend.position="bottom") p + theme(legend.position="none") # Remove legend
更改圖例中項目的順序
函數scale_x_discrete可用於將項目的順序更改爲“2”,“0.5”,“1”:
p + scale_x_discrete(limits=c("2", "0.5", "1"))
具有多個組的點圖
# Change dot plot colors by groups ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) + geom_dotplot(binaxis='y', stackdir='center') # Change the position : interval between dot plot of the same group p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) + geom_dotplot(binaxis='y', stackdir='center', position=position_dodge(0.8)) p
更改點圖顏色並添加框圖:
# Change colors p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) # Add box plots ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) + geom_boxplot(fill="white")+ geom_dotplot(binaxis='y', stackdir='center') # Change the position ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) + geom_boxplot(position=position_dodge(0.8))+ geom_dotplot(binaxis='y', stackdir='center', position=position_dodge(0.8))
修改顏色和主題
# Basic dot plot ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()+ geom_dotplot(binaxis='y', stackdir='center')+ labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+ theme_classic() # Change color by groups dp <-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_dotplot(binaxis='y', stackdir='center')+ labs(title="Plot of length by dose",x="Dose (mg)", y = "Length") dp + theme_classic()
手動更改填充顏色:
# Continuous colors dp + scale_fill_brewer(palette="Blues") + theme_classic() # Discrete colors dp + scale_fill_brewer(palette="Dark2") + theme_minimal() # Gradient colors dp + scale_fill_brewer(palette="RdBu") + theme_minimal()