ggplot 几何点,修改文本
ggplot geom point, modify text
我正在尝试使用 R stat 模块 prcomp 和 ggplot2 执行 pca 分析,示例数据如下所示。对于每种车型,到目前为止,我能够使用下面给出的代码生成三列数据。
df:
> head(car.df)
honda_1_smp honda_2_smp honda_3_smp audi_1_smp audi_2_smp audi_3_smp merc_1_smp merc_2_smp
s1 0.000289 0.000000 0.076095 0.056965 0.030314 0.000000 0.000000 0.028548
s2 1.588724 1.678821 0.795915 0.552910 0.503845 0.248782 0.201806 2.324172
s3 0.067802 0.068452 0.082904 0.014259 0.038896 0.044144 0.003634 0.167235
s4 0.000000 0.000000 0.000000 0.000000 0.000000 0.008724 0.000000 0.000000
s5 0.822612 1.137569 0.008302 0.025600 0.000000 0.000000 0.000000 0.000000
s6 0.025091 0.096847 0.000000 0.031416 0.024999 0.000000 0.012987 0.000000
代码:
carpca = prcomp(t(car.df), center=T)
summary(carpca)
car12 = data.frame(PC1=carpca$x[,1], PC2= carpca$x[,2], type=rownames(carpca$x))
ggplot(car12, aes(x=PC1 , y=PC2 , col=type)) +
geom_point() + geom_text(aes(label = type), hjust=0, vjust=0) +
xlab("PC1 89%") + ylab("PC2 77%") + ggtitle("car")
情节
问题
如何将我的所有副本 headers 分组为绘图和图例中的一种颜色和形状。
含义:对于奥迪和梅尔克,每辆本田都将具有相同的颜色和形状。
我会使用正则表达式 (gsub) 从 "type" 属性中删除复制 ID。
car12 = data.frame(PC1=carpca$x[,1], PC2= carpca$x[,2], type=gsub("_.*$", "", rownames(carpca$x)))
ggplot(car12, aes(x=PC1 , y=PC2 , col=type)) +
geom_point() + geom_text(aes(label = type), hjust=0, vjust=0) +
xlab("PC1 89%") + ylab("PC2 77%") + ggtitle("car")
我正在尝试使用 R stat 模块 prcomp 和 ggplot2 执行 pca 分析,示例数据如下所示。对于每种车型,到目前为止,我能够使用下面给出的代码生成三列数据。
df:
> head(car.df)
honda_1_smp honda_2_smp honda_3_smp audi_1_smp audi_2_smp audi_3_smp merc_1_smp merc_2_smp
s1 0.000289 0.000000 0.076095 0.056965 0.030314 0.000000 0.000000 0.028548
s2 1.588724 1.678821 0.795915 0.552910 0.503845 0.248782 0.201806 2.324172
s3 0.067802 0.068452 0.082904 0.014259 0.038896 0.044144 0.003634 0.167235
s4 0.000000 0.000000 0.000000 0.000000 0.000000 0.008724 0.000000 0.000000
s5 0.822612 1.137569 0.008302 0.025600 0.000000 0.000000 0.000000 0.000000
s6 0.025091 0.096847 0.000000 0.031416 0.024999 0.000000 0.012987 0.000000
代码:
carpca = prcomp(t(car.df), center=T)
summary(carpca)
car12 = data.frame(PC1=carpca$x[,1], PC2= carpca$x[,2], type=rownames(carpca$x))
ggplot(car12, aes(x=PC1 , y=PC2 , col=type)) +
geom_point() + geom_text(aes(label = type), hjust=0, vjust=0) +
xlab("PC1 89%") + ylab("PC2 77%") + ggtitle("car")
情节
问题
如何将我的所有副本 headers 分组为绘图和图例中的一种颜色和形状。 含义:对于奥迪和梅尔克,每辆本田都将具有相同的颜色和形状。
我会使用正则表达式 (gsub) 从 "type" 属性中删除复制 ID。
car12 = data.frame(PC1=carpca$x[,1], PC2= carpca$x[,2], type=gsub("_.*$", "", rownames(carpca$x)))
ggplot(car12, aes(x=PC1 , y=PC2 , col=type)) +
geom_point() + geom_text(aes(label = type), hjust=0, vjust=0) +
xlab("PC1 89%") + ylab("PC2 77%") + ggtitle("car")