Javier Hernando
Javier Hernando

Reputation: 368

Remove legend in ggplot

I am working with ggeffects package I have the following syntax

data_example <- structure(list(paciente = structure(c(6171, 6488, 6300, 6446, 
6489, 6445, 6473, 6351, 6212, 6387), label = "Paciente", format.spss = "F6.0"), 
    edad_s1 = structure(c(69, 62, 60, 71, 67, 59, 63, 66, 67, 
    70), label = "Edad", format.spss = "F3.0"), sexo_s1 = structure(c(1L, 
    2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L), .Label = c("Hombre", 
    "Mujer"), label = "Sexo", class = "factor"), grupo_int_v00 = structure(c(1L, 
    1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("A", "B"), label = "Grupo de intervención", class = "factor"), 
    time = c(0, 0, 0, 2, 2, 2, 1, 2, 1, 1), peso1 = c(89.9, 62, 
    91.5, 75.2, 68.2, 88.4, 93.6, 79, 88.3, 84.4), cintura1 = c(113, 
    90, 112, NA, 87.5, 116, 98.5, 104, 112.5, 108.5), tasis2_e = c(132, 
    132, 149, NA, 145, 137, 129, 152, 146, 129), tadias2_e = c(81, 
    58, 79, NA, 80, 60, 79, 87, 79, 68), p17_total = c(7, 9, 
    10, 10, 10, 10, 10, 7, 10, 11), geaf_tot = c(3412.59, 3524.48, 
    559.44, 5454.55, 4293.71, 839.16, 3146.85, 7552.45, 4335.66, 
    566.9), glucosa = c(102, 97, 89, NA, 88, 168, 104, NA, 114, 
    121), albumi = c(4.94, 4.68, 4.75, NA, 4.34, 5.06, 4.56, 
    NA, 5.06, 3.96), coltot = c(232, 253, 215, NA, 202, 287, 
    255, NA, 217, 147), hdl = c(59, 64, 68, NA, 71, 46, 61, NA, 
    40, 42), ldl_calc = c(143, 150, 127, NA, 114, NA, 170, NA, 
    143, 86), trigli = c(152, 195, 99, NA, 85, 378, 121, NA, 
    170, 93), hba1c = c(5.61, 5.66, 5.43, NA, 5.38, 8.14, 5.81, 
    NA, 6, 6.38), i_hucpeptide = c(988.91, 673.5, 1036.03, NA, 
    734.29, 1266.3, 610.9, NA, 1144.8, 672.08), i_hughrelin = c(1133.35, 
    1230.06, 1109.98, NA, 1064.79, 725.35, 1437.85, NA, 866.07, 
    822.83), i_hugip = c(2.67, 2.67, 2.67, NA, 2.67, 2.67, 2.67, 
    NA, 2.67, 2.67), i_huglp1 = c(145.43, 138.32, 194.14, NA, 
    99.37, 166.27, 218.33, NA, 184.04, 222.84), i_huglucagon = c(513.89, 
    357.35, 624.73, NA, 464.85, 448.49, 304.29, NA, 310.61, 426.52
    ), i_huinsulin = c(234.23, 229.06, 358.86, NA, 175.38, 466, 
    99.02, NA, 367.95, 77.33), i_huleptin = c(7898.28, 5211.27, 
    14670.25, NA, 7161.39, 3218.49, 2659.8, NA, 3766.01, 1207.58
    ), i_hupai1 = c(3468.4, 1977.9, 4101.1, NA, 1613.4, 2847.27, 
    2442.49, NA, 1953.26, 1752.88), i_huresistin = c(4783.28, 
    2676.05, 3064.57, NA, 2165.52, 3878.48, 8343.46, NA, 2822.68, 
    6496.73), i_huvisfatin = c(831.6, 649.45, 2270.65, NA, 1578.88, 
    9.63, 185.09, NA, 162.8, 8.64), col_rema = c(30, 39, 20, 
    NA, 17, NA, 24, NA, 34, 19), homa = c(1061.843, 987.503, 
    1419.491, NA, 685.931, 3479.467, 457.692, NA, 1864.28, 415.864
    ), i_pcr = c(0.05, NA, 0.27, NA, 0.03, 0.23, 0.04, NA, 0.09, 
    0.09), d_homa = c(NA, NA, NA, NA, -2.629, 33.042, -181.211, 
    NA, -929.683, -89.108), d_hughrelin = c(NA, NA, NA, NA, -213.59, 
    48.43, 95.27, NA, -228.62, -146.8), d_huinsulin = c(NA, NA, 
    NA, NA, 3.24, -68.79, -43.31, NA, -147.33, -7.46), d_hucpeptide = c(NA, 
    NA, NA, NA, 192.39, -263.54, -71.56, NA, -437.38, -215.44
    ), d_huglucagon = c(NA, NA, NA, NA, 38.99, -112.45, -10.75, 
    NA, -133.55, -259.73), d_huleptin = c(NA, NA, NA, NA, 409.76, 
    -1081.5, -1778.69, NA, -353.91, -679.7), d_huresistin = c(NA, 
    NA, NA, NA, 391.02, -155.41, -436.47, NA, -1137.79, -922.75
    ), d_huvisfatin = c(NA, NA, NA, NA, 457.54, -260.79, -341.02, 
    NA, -426.89, 0), d_glucosa = c(NA, NA, NA, NA, -2, 23, 3, 
    NA, -8, -13), d_coltot = c(NA, NA, NA, NA, -52, 36, -11, 
    NA, 15, -12), d_hdl = c(NA, NA, NA, NA, 1, 3, -1, NA, 1, 
    4), d_ldl_calc = c(NA, NA, NA, NA, -50, NA, -10, NA, 12, 
    -15), d_col_rema = c(NA, NA, NA, NA, -3, NA, 0, NA, 2, -1
    ), d_trigli = c(NA, NA, NA, NA, -14, 132, -1, NA, 8, -5), 
    d_hba1c = c(NA, NA, NA, NA, -0.11, -0.04, -0.18, NA, -1.76, 
    -0.67), d_tasis2_e = c(NA, NA, NA, NA, 0, 6, -1, 7, -21, 
    -9), d_tadias2_e = c(NA, NA, NA, NA, 0, 2, -8, 8, -10, -17
    ), d_peso1 = c(NA, NA, NA, -6, -2.3, 0.2, -11.4, 0.8, -4.1, 
    -9.3), d_cintura1 = c(NA, NA, NA, NA, -2.5, -4, -12.5, 6, 
    -3.5, -4.5), d_geaf_tot = c(NA, NA, NA, 699.31, 2055.95, 
    -2181.82, 1748.25, 3776.23, 867.13, -6593.94), d_p17_total = c(NA, 
    NA, NA, 1, 4, 5, 4, -5, 5, 2), d_hupai1 = c(NA, NA, NA, NA, 
    -185.03, 204.77, 202.01, NA, -1551.91, 57.2), d_hugip = c(NA, 
    NA, NA, NA, 0, 0, 0, NA, 0, 0), d_huglp1 = c(NA, NA, NA, 
    NA, -42.07, -163.02, 107.28, NA, -95.82, -87.5), d_pcr = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, -0.18, -0.22), ln_trigli = c(5.024, 
    5.273, 4.595, NA, 4.443, 5.935, 4.796, NA, 5.136, 4.533), 
    ln_homa = c(6.968, 6.895, 7.258, NA, 6.531, 8.155, 6.126, 
    NA, 7.531, 6.03), ln_hba1c = c(1.725, 1.733, 1.692, NA, 1.683, 
    2.097, 1.76, NA, 1.792, 1.853), ln_geaf_tot = c(8.135, 8.167, 
    6.327, 8.604, 8.365, 6.732, 8.054, 8.93, 8.375, 6.34), i_ratiolg = c(6.969, 
    4.237, 13.217, NA, 6.726, 4.437, 1.85, NA, 4.348, 1.468)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

The mixed model I have created following the syntax


lme_peso <- lme(peso1 ~ sexo_s1 + edad_s1 + poly(time, 2)*grupo_int_v00 + p17_total,
           random = ~ poly(time, 2)|paciente, control=lmeControl(opt="optim"),
           data = dat_longer, subset = !is.na(peso1), na.action =  na.omit)

And then to plot it

ggpredict(lme_peso, c("time [all]", "grupo_int_v00"), type="fixed") %>% 
    ggplot(aes(x = x, y = predicted, colour = group)) +
    geom_point() +
    geom_line() +
    stat_smooth(method = "loess",se = T) +
    labs(x = "time (months)", y = "Weight (kg)") +
  scale_color_manual(labels = c("Control", "Intervention"), values = c("orange", "green")) +
     geom_ribbon(aes(ymin = conf.low, ymax = conf.high, fill = F),alpha = 1/5) +
    scale_x_continuous(breaks = 0:2, labels = c(0, 6, 12)) 

When I supress the arguments of fill in geom_ribbon the fill stays black. But I don't know how to manage to keep just one legend with 2 groups (Control and Intervention). I have the extra-added legend (with F in this case)

Thanks in advance

enter image description here

Upvotes: 1

Views: 507

Answers (1)

Marco_CH
Marco_CH

Reputation: 3294

I couldn't run your code, but I rebuilt it with iris.

Like Matt suggested, one thing would be, remove fill=F:

ggplot(data=iris, aes(x = SepalLength , y = PetalLength, group=Name)) +
  geom_point() +
  geom_line() +
  stat_smooth(method = "loess",se = T, aes(color=Name)) +
  geom_ribbon(aes(ymin = 1, ymax = 3),alpha = 1/5) +
  scale_x_continuous(breaks = 0:2, labels = c(0, 6, 12)) 

Or if you need it for some reason, use guides(fill="none"):

ggplot(data=iris, aes(x = SepalLength , y = PetalLength, group=Name)) +
  geom_point() +
  geom_line() +
  stat_smooth(method = "loess",se = T, aes(color=Name)) +
  geom_ribbon(aes(ymin = 1, ymax = 3, fill=FALSE),alpha = 1/5) +
  scale_x_continuous(breaks = 0:2, labels = c(0, 6, 12)) +
  guides(fill="none")

Output:

enter image description here

Upvotes: 1

Related Questions