I want to change the colors of hline "a, d, j, " to red , so what should i do?!!!

library(tidyverse)
set.seed(13)
names<-letters[1:10]
var1<-rnorm(10,25,5)
dat5<-tibble(names,var1)

Plot

ggplot(dat5, aes(x=reorder(names,var1), y=var1)) +
geom_segment( aes( xend=names, yend=0), color="darkolivegreen4") +
geom_point(color="chartreuse4") +
labs(y= "Variable 1", x="")
image

Do you mean something like this?

library(tidyverse)
set.seed(13)
names<-letters[1:10]
var1<-rnorm(10,25,5)
dat5<-tibble(names,var1)

ggplot(dat5, aes(x=reorder(names,var1), y=var1)) +
  geom_segment( aes( xend=names, yend=0), color="darkolivegreen4") +
  geom_point(color="chartreuse4") +
  geom_segment(aes(x="i",xend="c",y=0,yend=0),color="red")+
  labs(y= "Variable 1", x="")

Created on 2022-01-05 by the reprex package (v2.0.1)

1 Like

I think Xuan wants something like this

library(tidyverse)
set.seed(13)
names<-letters[1:10]
var1<-rnorm(10,25,5)
dat1<-tibble(names,var1)

dat2 <-   dat1 %>%  mutate(xx = case_when(names == "a" ~ 'red'
                                          ,names == "d" ~ 'red'
                                          ,names == "j" ~ 'red'
                                          ,TRUE ~ 'yellow'
) )


ggplot(dat2, aes(x=reorder(names,var1), y=var1, colour = xx)) +
  geom_segment( aes( xend=names, yend=0)) +
  geom_point() + 
  labs(y= "Variable 1", x="")

but to get the colours correct I think I need to use scale_fill_manual() and I don't have time futz around with it at the moment

1 Like

library(tidyverse)
set.seed(13)
names<-letters[1:10]
var1<-rnorm(10,25,5)
var2 <- c("a", "b", "b","a", "b", "b", "b", "b", "b", "a")
dat5<-tibble(names,var1, var2)

ggplot(dat5, aes(x=reorder(names,var1), y=var1, color = var2)) +
geom_segment( aes( xend=names, yend=0)) +
geom_point() +
scale_color_manual(values = c("red", "chartreuse4")) +
labs(y= "Variable 1", x="")

1 Like

Yepppp, thank for your help but can you tell me how to make annotations about color not appear in the chart

Thank youuuuu, to me, your code is the easiest to understand, but i want to uncomment the color in plot, only plot like picture i posted yesterday, without caption.

I wanna change the circled lines to red
image

# define the ones you want to highlight
highlight = c("a", "d", "j")
  
# add an extra column
dat2 <-   dat1 %>%  
  mutate(to_highlight = if_else(names %in% highlight,
                                "yes", "no"))


ggplot(dat2, aes(x=reorder(names,var1), 
                 y=var1,
                 #define the colour based on highlight column
                 colour = to_highlight)) +
  geom_segment( aes( xend=names, yend=0)) +
  geom_point() + 
  labs(y= "Variable 1", x="") +
  # define the colours to be used
  scale_colour_manual(values = c("yes" = "red",
                               "no" =  "chartreuse4"))  + 
  # hide the legend
  theme(legend.position = "none")

1 Like

Greatttt!!!! This is what i want. Thank you so much and thank for explaining code, so perspicuous.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.