2

I'd like my graph to start at y=0. Hence, all bars with a zero value should not be shown in the figure. However, bars with zero values are showing in my graph as if they had a negative value. See my (unwanted) graph.

This is the data series used in the graph:

data1$Node
[1] "EQU" "GYP" "NON" "NOZ" "BIG" "NZB" "ORS" "KAF"

data1$Flow
[1] "0.0e+00" "0.0e+00" "0.0e+00" "0.0e+00" "0.0e+00" "0.0e+00" "0.0e+00" "1.2e-05"

Something similar will happen if I set the ymin limit to 0: in this case bars don't appear but the 0 is not set at bottom figure 2.

I've tried to solve the problem looking a previous questions but nothing seems to work. The code runs but it simply does not do what I'd like it to do. See code below:

      cond1<-which(datagraph3$quality==targetQuality)
      Flow<-datagraph3$budget[,cond1]
      data1<-as.data.frame(Flow)
      data1$Node<-rownames(data1)
      data1$Flow<-formatC(data1$Flow, format = "e", digits = 1)
      data1$Node<-c(data1$Node[2],data1$Node[3],data1$Node[5],data1$Node[6],
                    data1$Node[1],data1$Node[7],data1$Node[8],data1$Node[4])
      ymin=min(data1$Flow)
      ymax=max(data1$Flow)
      barp1<-ggbarplot(data1, x="Node", y="Flow",
                       ylab=datagraph3$unit[cond1], xlab="Node",
                       color="white", fill="grey", palette="jco",
                       label=TRUE) + theme(plot.margin = margin(1,0.5,0.5,0.5, "cm")) 
      #+ scale_y_discrete(expand = c(ymin, ymax), limits = c(0, ymax), breaks = data1$Flow)
      print(barp1+ylim(ymin,ymax)) # + font("x.text", size=7)
Dan Adams
  • 4,971
  • 9
  • 28
  • Welcome to SO! Help us help you by sharing some of the data you're trying to plot. You can use `dput(head(datagraph3, 10))` and add the output of that to your question. – Dan Adams Feb 17 '22 at 17:33
  • Why are you reformatting your data into scientific notation and storing as text? It makes more sense to leave it as `numeric` – Dan Adams Feb 17 '22 at 17:54
  • 1
    Oh that's your problem. Because you converted you `y` variable to a discrete variable by making it `character` setting numeric axis limits doesn't make sense. Just treat it as a number and format the axis labels to your liking. – Dan Adams Feb 17 '22 at 17:57
  • such a tiny mistake. Love you Dan <33333333 – Thais Jiménez Feb 17 '22 at 18:03

1 Answers1

1

The issue is that you converted your y variable into a discrete variable by making it into a character using formatC(). Therefore your y axis is discrete and setting numeric axis limits doesn't make sense.

Instead it's better to use it as a numeric and then you can format your axis labels in ggplot2 as needed. Also note that I use reorder() inside the ggplot(aes()) to order the bars. Other methods to do this include setting factor levels prior to plotting.

library(tidyverse)

data1 <- structure(list(Node = c("EQU", "GYP", "NON", "NOZ", "BIG", "NZB", "ORS", "KAF"), Flow = c("0.0e+00", "0.0e+00", "0.0e+00", "0.0e+00", "0.0e+00", "0.0e+00", "0.0e+00", "1.2e-05")), class = "data.frame", row.names = c(NA, -8L))

# current plot
data1 %>% 
  ggplot(aes(Node, Flow)) +
  geom_col()

# convert to numeric to get desired plot
data1 %>% 
  mutate(Flow = as.numeric(Flow)) %>% 
  ggplot(aes(reorder(Node, Flow), Flow)) +
  geom_col()

Created on 2022-02-17 by the reprex package (v2.0.1)

Dan Adams
  • 4,971
  • 9
  • 28