如何在fviz_pca_var标签中包含下标?

huangapple go评论140阅读模式
英文:

How do I include subscripts with fviz_pca_var labels?

问题

我正在使用fviz_pca_var来可视化我的PCA,我想要为某些变量包括下标。如何使它们正确显示?

另外,有没有更简单的方法来翻转y轴,使值倒置?(我仍希望-1位于左侧)。

df <- structure(list(`P[1]` = c(-0.9, -1, -1.1, -1.2, -1.28, -1.35, -1.41, -1.45, -1.47, -1.47, -1.45, -1.41, -1.35, -1.27, -1.17, -1.06, -0.93, -0.79, -0.64, -0.49, -0.33, -0.17, -0.01, 0.14, 
                        0.29, 0.44, 0.57, 0.68, 0.78, 0.86, 0.92, 0.97, 1, 1.02, 1.03, 1.03, 1.02, 1.02, 1.01, 1, 1, 0.99, 0.98, 0.96, 0.94, 0.92, 0.89, 0.86, 0.82, 0.79, 0.75), 
                     `Z[2]` = c(0.56, 0.5, 0.44, 0.38, 0.32, 
                                0.27, 0.22, 0.19, 0.18, 0.18, 0.22, 0.27, 0.36, 0.47, 0.6, 0.74, 0.88, 1.02, 1.15, 1.25, 1.32, 1.35, 1.33, 1.25, 1.11, 0.91, 0.67, 0.38, 0.06, -0.27, -0.61, -0.93, -1.22, -1.46, -1.66, -1.79, 
                                -1.86, -1.87, -1.82, -1.72, -1.56, -1.37, -1.14, -0.89, -0.62, -0.34, -0.06, 0.23, 0.51, 0.8, 1.08), 
                     `C3[1]` = c(-0.16, -0.14, -0.12, -0.1, -0.25, -0.53, -0.29, -0.83, -0.69, -0.06, -0.98, 
                                 -0.53, -0.8, 1.17, 0.64, 0.09, 0.61, 1.97, 0.84, 1.85, -0.98, -0.67, -0.82, -1, -0.47, -1.01, -0.79, -0.97, -1.33, -1.48, -1.59, -1.71, -1.8, -1.88, -1.94, -2.09, -1.96, -1.96, -1.95, -1.97, 
                                 -1.88, -1.82, -1.63, -1.52, -1.54, -1.4, -1.42, -1.29, -1.19, -0.83, -0.69), 
                     `C3[2]` = c(0.77, 0.79, 0.81, 0.82, 0.79, 0.71, 0.78, 0.63, 0.67, 0.86, 0.59, -0.34, 0.65, 1.23, 1.53, 1.7, 0.99, 
                                 2.07, 1.71, 3.01, -0.53, -0.53, 0.54, 0.43, 0.52, -0.59, 0.25, 0.1, -0.5, -0.57, -0.54, -0.57, -0.55, -0.55, -0.56, -0.83, -0.46, -0.46, -0.46, -0.61, -0.49, -0.51, -0.2, -0.13, -0.39, -0.26, 
                                 -0.54, -0.44, -0.44, 0.22, 0.34)), 
                row.names = c(NA, -51L), class = c("tbl_df", "tbl", "data.frame"))
pca <- princomp(df, scores = TRUE)
fviz_pca_var(pca, title = "", repel = TRUE, ggtheme = theme_bw()) +
  scale_x_reverse() +
  scale_y_continuous(limits = c(-0.2, 0.6))

如何在fviz_pca_var标签中包含下标?

英文:

I have a PCA that I'm visualizing with fviz_pca_var and I want to include subscripts for some of my variables. How do I get these to display properly?

Also, is there any easier way to flip the y-axis so the values are inverted? (I still want -1 to be on the left).

df &lt;- structure(list(`P[1]` = c(-0.9, -1, -1.1, -1.2, -1.28, -1.35, -1.41, -1.45, -1.47, -1.47, -1.45, -1.41, -1.35, -1.27, -1.17, -1.06, -0.93, -0.79, -0.64, -0.49, -0.33, -0.17, -0.01, 0.14, 
0.29, 0.44, 0.57, 0.68, 0.78, 0.86, 0.92, 0.97, 1, 1.02, 1.03, 1.03, 1.02, 1.02, 1.01, 1, 1, 0.99, 0.98, 0.96, 0.94, 0.92, 0.89, 0.86, 0.82, 0.79, 0.75), 
`Z[2]` = c(0.56, 0.5, 0.44, 0.38, 0.32, 
0.27, 0.22, 0.19, 0.18, 0.18, 0.22, 0.27, 0.36, 0.47, 0.6, 0.74, 0.88, 1.02, 1.15, 1.25, 1.32, 1.35, 1.33, 1.25, 1.11, 0.91, 0.67, 0.38, 0.06, -0.27, -0.61, -0.93, -1.22, -1.46, -1.66, -1.79, 
-1.86, -1.87, -1.82, -1.72, -1.56, -1.37, -1.14, -0.89, -0.62, -0.34, -0.06, 0.23, 0.51, 0.8, 1.08), 
`C3[1]` = c(-0.16, -0.14, -0.12, -0.1, -0.25, -0.53, -0.29, -0.83, -0.69, -0.06, -0.98, 
-0.53, -0.8, 1.17, 0.64, 0.09, 0.61, 1.97, 0.84, 1.85, -0.98, -0.67, -0.82, -1, -0.47, -1.01, -0.79, -0.97, -1.33, -1.48, -1.59, -1.71, -1.8, -1.88, -1.94, -2.09, -1.96, -1.96, -1.95, -1.97, 
-1.88, -1.82, -1.63, -1.52, -1.54, -1.4, -1.42, -1.29, -1.19, -0.83, -0.69), 
`C3[2]` = c(0.77, 0.79, 0.81, 0.82, 0.79, 0.71, 0.78, 0.63, 0.67, 0.86, 0.59, -0.34, 0.65, 1.23, 1.53, 1.7, 0.99, 
2.07, 1.71, 3.01, -0.53, -0.53, 0.54, 0.43, 0.52, -0.59, 0.25, 0.1, -0.5, -0.57, -0.54, -0.57, -0.55, -0.55, -0.56, -0.83, -0.46, -0.46, -0.46, -0.61, -0.49, -0.51, -0.2, -0.13, -0.39, -0.26, 
-0.54, -0.44, -0.44, 0.22, 0.34)), 
row.names = c(NA, -51L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
pca &lt;- princomp(df, scores = TRUE)
fviz_pca_var(pca, title = &quot;&quot;, repel = TRUE, ggtheme = theme_bw()) +
scale_x_reverse() +
scale_y_continuous(limits = c(-0.2, 0.6))

如何在fviz_pca_var标签中包含下标?

答案1

得分: 2

只需在fviz_pca_var函数中添加parse = TRUE参数。

fviz_pca_var(pca,
             title = "",
             repel = TRUE,
             #这是我所做的唯一更改
             parse = TRUE,
             ggtheme = theme_bw())
# 你的其余代码

如何在fviz_pca_var标签中包含下标?

英文:

You just need to add the parse = TRUE argument to the fviz_pca_var function.

fviz_pca_var(pca,
title = &quot;&quot;, 
repel = TRUE, 
#This is the only change I made 
parse = T,
ggtheme = theme_bw()) 
# Rest of your code

如何在fviz_pca_var标签中包含下标?

答案2

得分: 1

上面的回答是正确的,但让我感到烦恼的是标签和线条重叠在一起。我认为您可以在fviz_pca_var()...参数中添加nudge_y,但实际上这不会传递给geom_text(),所以没有效果。

如果您想要使用nudge_y,您需要首先绘制没有标签的图,然后再添加它们。为此,您需要从您的pca对象(具有类别princomp)创建一个data.frame,方式与factoextra::fviz()相同:

dat <- facto_summarize(pca, element = "var", axes = c(1, 2), result = c("coord", "contrib", "cos2"))

然后,您可以通过设置geom = "arrow"来绘制没有文本的图,自己添加文本并根据需要调整nudge_y参数:

fviz_pca_var(pca, title = "", geom = "arrow", ggtheme = theme_bw()) +
    scale_y_continuous(limits = c(-0.2, 0.6)) +
    geom_text(
        data = dat,
        aes(x = Dim.1, y = Dim.2, label = name),
        parse = TRUE,
        nudge_y = 0.02,
    )

如何在fviz_pca_var标签中包含下标?

英文:

The above answer is correct but it annoyed me that the labels and the lines overlap. I thought you could add nudge_y to the ... arguments of the fviz_pca_var() but this does not actually get passed through to geom_text(), so it has no effect.

If you want to use nudge_y, you need to initially draw the plot without labels, then add them. To do that you need to create a data.frame from your pca object (which has class princomp), in the same way as factoextra::fviz():

dat &lt;- facto_summarize(pca, element = &quot;var&quot;, axes = c(1, 2), result = c(&quot;coord&quot;, &quot;contrib&quot;, &quot;cos2&quot;))

Then you can draw the plot without text by setting geom = &quot;arrow&quot;, add the text yourself and adjust the nudge_y parameter as desired:

fviz_pca_var(pca, title = &quot;&quot;, geom = &quot;arrow&quot;, ggtheme = theme_bw()) +
    scale_y_continuous(limits = c(-0.2, 0.6)) +
    geom_text(
        data = dat,
        aes(x = Dim.1, y = Dim.2, label = name),
        parse = TRUE,
        nudge_y = 0.02,
    )fviz_pca_var(pca, title = &quot;&quot;, geom = &quot;arrow&quot;, ggtheme = theme_bw()) +
    scale_y_continuous(limits = c(-0.2, 0.6)) +
    geom_text(
        data = dat,
        aes(x = Dim.1, y = Dim.2, label = name),
        parse = TRUE,
        nudge_y = 0.02,
    )

如何在fviz_pca_var标签中包含下标?

huangapple
  • 本文由 发表于 2023年7月5日 01:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定