Лесной участок - как обозначить коэффициенты? - PullRequest
0 голосов
/ 30 апреля 2020

У меня есть этот лесной участок, но. Я хотел бы: а) добавить метки коэффициентов, чтобы я мог видеть фактическое число, и б) заголовок графика.

library(jtools)
library(ggstance)
library(broom.mixed)

## Plot Coefficients 
test_plot <- plot_summs(FE_model, scale = TRUE, inner_ci_level = .95, robust = TRUE, coefs = c("Age" = "age", "Age Squared" = "age_sq", "Primary Education" = "highest_education1", "Secondary Education" = "highest_education2", "Post-Secondary Education" = "highest_education3", "Logged Household Income" = "hh_income_log"))


## Increase font size, etc.  
apatheme=theme_bw()+
  theme(panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        panel.border=element_blank(),
        axis.line=element_line(),
        text=element_text(family='Helvetica'),
        legend.title=element_blank(), 
        axis.text=element_text(size=12),
        axis.title=element_text(size=12),
        legend.text = element_text(size = 12))


## Final Plot 
test_plot + apatheme

enter image description here

1 Ответ

0 голосов
/ 01 мая 2020

Вы можете извлечь значения Estimate, используя функцию summ из пакета jtools. Затем вы можете сгенерировать кадр данных для использования с geom_text.

Начиная с этого примера:

library(jtools)
library(ggstance)
library(broom.mixed)
states <- as.data.frame(state.x77)
fit1 <- lm(Income ~ Frost + Illiteracy + Murder +
             Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))

Чтобы извлечь оценочные значения, вы можете сделать:

label_data <- as.data.frame(summ(fit1))
label_data$Coeff = str_replace_all(rownames(label_data), c("Frost" = "Frost Days", "Illiteracy" = "% Illiterate",
                       "Murder" = "Murder Rate"))


                     Est.         S.E.      t val.         p        Coeff
(Intercept)  242.10962043 1.162446e+04  0.02082761 0.9834818  (Intercept)
Frost         -0.04152112 2.705093e+00 -0.01534924 0.9878262   Frost Days
Illiteracy  -144.00659569 4.480265e+02 -0.32142425 0.7494831 % Illiterate
Murder        -3.86852803 5.729288e+01 -0.06752197 0.9464866  Murder Rate
Population     0.03319624 2.644272e-02  1.25540184 0.2162728   Population
Area           0.00185163 6.030556e-03  0.30704129 0.7603292         Area
`Life Exp`    36.34460105 1.573097e+02  0.23103851 0.8184067   `Life Exp`
`HS Grad`     29.75715170 2.494897e+01  1.19272074 0.2396736    `HS Grad`

И, наконец, вы можете построить свою модель и добавить заголовок и отметьте оценочные значения, выполнив:

library(ggplot2)

plot_summs(fit1,
           coefs = c("Frost Days" = "Frost", "% Illiterate" = "Illiteracy",
                     "Murder Rate" = "Murder"),
           scale = TRUE, robust = TRUE)+
  geom_text(inherit.aes = FALSE, 
            data = subset(label_data, Coeff %in% c("Frost","Illiteracy","Murder")),
            aes(x = `Est.`, y = Coeff, label = round(`Est.`,3)), vjust = -1)+
  ggtitle("Title")

enter image description here

Исходя из предоставленного вами кода, вы должны сделать что-то вроде этого:

label_data <- summ(FE_model, robust = TRUE)$coeftable
label_data$Coeff <- str_replace_all(rownames(label_data, c("age" = "Age", "age_sq" = "Age Squared",
                                                 "highest_education1"="Primary Education",
                                                 "highest_education2"= "Secondary Education", 
                                                 "highest_education3" = "Post-Secondary Education", 
                                                 "hh_income_log" = "Logged Household Income")))

test_plot <- plot_summs(FE_model, scale = TRUE, inner_ci_level = .95, robust = TRUE, 
                        coefs = c("Age" = "age", "Age Squared" = "age_sq",
                                  "Primary Education" = "highest_education1",
                                  "Secondary Education" = "highest_education2", 
                                  "Post-Secondary Education" = "highest_education3", 
                                  "Logged Household Income" = "hh_income_log"))+
  geom_text(inherit.aes = FALSE, 
            data = subset(label, Coeff %in% c("Age","Age Squared","Primary Education","Secondary Education", 
                                          "Post-Secondary Education","Logged Household Income")),
            aes(x = `Est.`, y = Coeff, label = round(`Est.`,3)), vjust = -1)+
  ggtitle("Title")

Это отвечает на ваш вопрос?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...