0

I took an interesting sample of ggplot here and remade it for my data
My data is excel file

`

col <- c("Low weight", "very low weight", "Extra Very Low weight", "Cesarean section", "premature")
> ORpos <- c(6.8, 4.5,0.4, 4.5, 3.9)
> CI1 <- c(0.6, 0.4, 0, 0.2, 0.4)
> CI2 <- c(74, 56, 32, 20, 2.4)
> ORneg <- c(4.8, 15, 6, 6, 4)
> CI3 <- c(0.6, 0.4, 0, 0.2, 0.4)
> CI4 <- c(56, 43, 21, 10, 5)
> md4 <- data.frame(col,ORpos, CI1, CI2, ORneg, CI3, CI4)
 
plot3 <- ggplot(md4, aes(x = col)) +
  geom_col( 
    aes(y = Orp, fill = 'Orp')) +geom_text(aes(y=Orp, label = paste(Orp, " [", CI1," ; ", CI2, "]", sep = ""), hjust = -1))+
  geom_col( 
    aes(y = -Orn, fill = 'Orn')) + geom_text(aes(y=-Orn, label = paste(Orn, " [", CI3," ; ", CI4, "]", sep = ""), hjust = 1))+
  coord_flip() +
  scale_y_continuous(limits = c(-30, 30))

plot3

But this is where my understanding of R ended completely.
my plot

  1. Why is order my variables in the plot not order as in the table ???

2 What to do to data labels would be on the right and left edges of the plot or at least exactly under each other

LLA
  • 1
  • 1
  • 1
    Welcome to SO. It would be easier to help you if you provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data shared via `dput()`. – stefan Sep 19 '22 at 10:25
  • 1
    Please share your sample data as copy/pasteble text, preferably in valid R syntax as produced by eg., `dput(md4[1:5, ])` for the first 5 rows. We can't test solutions on a screenshot of a table of data. – Gregor Thomas Sep 19 '22 at 13:15
  • 1
    As for the ordering, `ggplot` will implicitly coerce categorical variables to `factor` class, and the default ordering for `factor` levels is alphabetical. You can set your own order--if you want the order as the first appear in the data than `md4$col = factor(md4$col, levels = unique(md4$col))` will do it. For other orders [you can find lots of examples](https://stackoverflow.com/search?q=%5Bggplot2%5D+%5Br-faq%5D+order). – Gregor Thomas Sep 19 '22 at 13:18
  • Orp is not in the dataframe at all – Dimitrios Zacharatos Sep 19 '22 at 14:12

1 Answers1

0

For your first issue of reordering you need to transform your character column to factors (as Gregor pointed out) via md4$col = factor(md4$col, levels = unique(md4$col)) or just md4$col = factor(md4$col, levels = md4$col) as apparently you don't have repeating values in your column.

For the second issue as far as I know hjust should be between 0 to 1 for left and right justified text relative to your datapoint respectively, so you may want to set it 0 for your ORpos and 1 for ORneg data and then extend your scales to fit in all text, hope this helps:

md4$col = factor(md4$col, levels = rev(md4$col))

ggplot(md4, aes(x = col)) +
  geom_col( aes(y = ORpos, fill = 'Positive culter')) +
  geom_text(aes(y=7, label = paste(ORpos, " [", CI1,"; ", CI2, "]", sep = " "), hjust = 0))+
  geom_col( aes(y = -ORneg, fill = 'Negative culter')) +
  geom_text(aes(y=-7, label = paste(ORneg, " [", CI3,"; ", CI4, "]", sep = " "), hjust = 1))+
  coord_flip() +
  scale_y_continuous(limits = c(-10, 10), breaks = c(-10:10))
Meisam
  • 601
  • 1
  • 3
  • 16
  • md4$col = factor(md4$col, levels = md4$col (!!!) This is working! For unknown fo me reason the order is the reverse of the table, but it works! – LLA Sep 19 '22 at 14:55
  • If you want to reverse the order just use `levels=rev(md4$col)` – Meisam Sep 19 '22 at 15:14
  • I have added data frame. I am using different options hajust. But the labels "dance" relative to the bars. Not readable. – LLA Sep 19 '22 at 15:14
  • plot3 <- ggplot(md4, aes(x = col)) + geom_col( aes(y = Orp, fill = 'Positive culter')) +geom_text(aes(y=3, label = paste(Orp, " [", CI1,"; ", CI2, "]", sep = " ", "p=", p1), hjust = 0))+ geom_col( aes(y = -Orn, fill = 'Negative culter')) + geom_text(aes(y=-3, label = paste(Orn, " [", CI3,"; ", CI4, "]", sep = " ", "p=", p2), hjust = 1))+ coord_flip() + scale_y_continuous(limits = c(-10, 10), breaks = c(-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7) ) plot3 [Did I break anything important? replaced y with a number, not the value of a variable] – LLA Sep 19 '22 at 16:03
  • Saw your table, glad that I could be of a help. But your code in the comment is still not reproducible what are `p1` and `p2`? I have polished it for you and updated my answer, take a look. good luck! – Meisam Sep 20 '22 at 03:25