Change the position of text and color of positive and negative bars

Hello,

If someone can please help me change the position (name) of the bars, like if i can move them inside the plot so that i will have more space for the image and also how i can change the color for negative bars.
Here is my code:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.4
df <- read.csv("F:/Chapter2_Figures/penalty_lift.csv")
colnames(df)[1] = "Attribute"
colnames(df)[2] = "Score"
df
#>        Attribute  Score
#> 1 Cookedpotato_F  2.035
#> 2 Cookedpotato_A  1.703
#> 3       Smooth_T  1.199
#> 4        Moist_T  0.853
#> 5        Dense_T -0.378
#> 6          Dry_T -0.848
#> 7        Mealy_T -1.017
#> 8    Rawpotato_A -1.130
#> 9    Rawpotato_F -1.567
df$Attribute <- factor(df$Attribute, levels = c("Rawpotato_F", "Rawpotato_A",
                                                "Mealy_T", "Dry_T",
                                                "Dense_T", "Moist_T", "Smooth_T",
                                                "Cookedpotato_A", "Cookedpotato_F"))
ggplot(df, aes(x=Attribute, y=Score)) +
  geom_bar(stat="identity") + coord_flip()

Created on 2021-03-27 by the reprex package (v1.0.0)

library(tidyverse)
data<- data.frame(group = paste("something_",1:6,sep=""),
                  value =c(5, 6, 12, -10, -17, -5))

ggplot(data, aes(x=group, y=value)) +
  geom_bar(stat="identity", aes(fill = ifelse(value<0, "red", "blue"))) + 
  geom_text(data = data, aes(label = group ,y = 0, x = group, 
                             vjust = ifelse(value >= 0, 0.5, 0.5),
                             hjust = ifelse(value>= 0, 1.05, -0.05)))+
                                  theme(axis.text.y = element_blank(),
                                              axis.ticks = element_blank(), 
                                        legend.position = "none")+coord_flip()+labs(x= "")

image

2 Likes

This topic was automatically closed 7 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.