how to change the y-axis to logarithmic in a barplot ggplot

197 views
Skip to first unread message

Maria Lathouri

unread,
Jul 16, 2023, 5:30:40 PM7/16/23
to Ggplot2
Dear all, 

I am having the following dataset
fc<-
ID    values    databases
Al    0.15    phreeqc
Al    0.6    carbfix
Al    0.47    actual
Ca    7.2    phreeqc
Ca    7.2    carbfix
Ca    0.3    actual
Na    14.4    phreeqc
Na    84    carbfix
Na    106    actual
Cl    22    phreeqc
Cl    21.9    carbfix
Cl    72.1    actual
Fe    0.05    phreeqc
Fe    0.43    carbfix
Fe    1.25    actual
Mg    0.35    phreeqc
Mg    0.17    carbfix
Mg    0.08    actual
SO4    0    phreeqc
SO4    0    carbfix
SO4    416    actual

As you can see, the values range from 0 to 400. I want it to plot it in bars; when I am plotting it as you can imagine the values near zero don't show at all. So I am trying to make the y axis logarithmic. I have created the following code but it doesn't work. I get the bars with zero above and the others on top. 

ggplot(fc, aes(x = Temp, y = mean, fill = Glass)) + 
geom_bar(stat = "identity", position = "dodge", aes(y=log(mean))) 
+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.position = c(0.45, 0.85), legend.title = element_blank()) 
+ scale_fill_brewer(palette = "Dark2") + scale_color_brewer(palette = "Dark2")

I would very much appreciate your help. I am stuck. 

Kind regards,
Maria
1689542973237blob.jpg

Konstantinos L. Papageorgiou

unread,
Jul 17, 2023, 3:48:35 AM7/17/23
to ggp...@googlegroups.com

Hi Maria,

Check out
http://www.sthda.com/english/wiki/ggplot2-axis-scales-and-transformations


sp + scale_x_continuous(trans='log2') +
  scale_y_continuous(trans='log2')

Maria Lathouri

unread,
Jul 17, 2023, 5:24:18 AM7/17/23
to Konstantinos Vlachopoulos, Ggplot2
Dear Konstantinos,

Thank you very much for this. Yes, it works pretty well. You are right , there is a high variation and yes, log of values close to zero gives negative ones; but your suggestion gives me a clear tendency of the results.

Very much appreciated.

Kind regards,
Maria

Στις Δευτέρα, 17 Ιουλίου 2023, 01:16:50 πμ EEST, ο χρήστης Konstantinos Vlachopoulos <kostisvla...@gmail.com> έγραψε:


Dear Maria, 

The log transformation of a close to zero decimal yields a negative infinite number, such as log_base10(0.6)=-0.51.
If your experimental design does not have a major flaw and you can comprehend the results, the square root transformation provides an alternative solution. 

The problem rises due to the huge variation that you have in your dataset.

I tried the following code and it yields to a decent graph. 

ggplot(fc, aes(x = Temp, y = Mean, fill = Glass)) + geom_bar(stat = "identity", position = "dodge", aes(y=sqrt(Mean))) + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.position = c(0.45, 0.85), legend.title = element_blank()) + scale_fill_brewer(palette = "Dark2") + scale_color_brewer(palette = "Dark2”)


Hope this helps, 

K

PastedGraphic-1.tiff


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/511435038.1260853.1689543020404%40mail.yahoo.com.
<1689542973237blob.jpg>

Konstantinos Vlachopoulos
Environmental Scientist, MSc Environmental Management
PhD Student in Population Ecology 

Laboratory of Ecosystem Management and Biodiversity
Department of Agriculture Crop Production and Rural Environment
University of Thessaly
τηλ: 6946054670



unnamed.jpg

Hadley Wickham

unread,
Jul 17, 2023, 11:04:50 AM7/17/23
to Maria Lathouri, Konstantinos Vlachopoulos, Ggplot2
If you do really want a log scale, then you can use a display other than a bar chart.

To start, lets make your existing code reproducible:

library(tidyverse)

fc <- tribble(
  ~Temp, ~Mean, ~Glass,
  "Al", 0.15, "phreeqc",
  "Al", 0.6, "carbfix",
  "Al", 0.47, "actual",
  "Ca", 7.2, "phreeqc",
  "Ca", 7.2, "carbfix",
  "Ca", 0.3, "actual",
  "Na", 14.4, "phreeqc",
  "Na", 84, "carbfix",
  "Na", 106, "actual",
  "Cl", 22, "phreeqc",
  "Cl", 21.9, "carbfix",
  "Cl", 72.1, "actual",
  "Fe", 0.05, "phreeqc",
  "Fe", 0.43, "carbfix",
  "Fe", 1.25, "actual",
  "Mg", 0.35, "phreeqc",
  "Mg", 0.17, "carbfix",
  "Mg", 0.08, "actual",
  "SO4", 0, "phreeqc",
  "SO4", 0, "carbfix",
  "SO4", 416, "actual"
)

ggplot(fc, aes(x = Temp, y = Mean, fill = Glass)) +
  geom_col(position = "dodge") +
  scale_fill_brewer(palette = "Dark2") +

# Instead of a bar chart, we could use a scatterplot:
ggplot(fc, aes(x = Temp, y = Mean, colour = Glass)) +
  geom_point() +
  scale_color_brewer(palette = "Dark2") +
  scale_y_log10()

# That more clearly reveals that a couple of value are 0
# So you'll have to think about how you want to handle those
# One option is to add a small positive offset to every value:
# There's a little bit about this at https://marcfbellemare.com/wordpress/12856

ggplot(fc, aes(x = Temp, y = Mean + 0.1, colour = Glass)) +
  geom_point() +
  scale_color_brewer(palette = "Dark2") +
  scale_y_log10()

# The overall message of the plot is pretty dilute so I also tried some
# minor tweaks to make it more clear. I don't think they were particularly
# successful, but you might like to see them

ggplot(fc, aes(x = fct_reorder(Temp, Mean + 0.1), y = Mean + 0.1, colour = Glass)) +
  geom_line(colour = "grey80", aes(group = Glass)) +
  geom_point() +
  scale_color_brewer(palette = "Dark2") +
  scale_y_log10()

Hadley





--

Maria Lathouri

unread,
Jul 17, 2023, 3:15:41 PM7/17/23
to Hadley Wickham, Konstantinos Vlachopoulos, Ggplot2
Dear Hadley,

Many thanks for this; yes the points works fine so all the values are visible. I really like what the fc_reorder does. I will play around to see if I can make it show better. 

Kind regards,
Maria 
> Laboratory of Ecosystem Management and BiodiversityDepartment of Agriculture Crop Production and Rural EnvironmentUniversity of Thessalyτηλ: 6946054670
>
>
>
>  
>
>
>
>
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>  
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/1028220942.1481561.1689585833349%40mail.yahoo.com.
>


--
http://hadley.nz


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/CABdHhvF9ZYJ1STTqiwTj7o_g8sAqivX3mVF7mJ0W7E%2BpKZN1wg%40mail.gmail.com.

Osorio Matucurane

unread,
Jul 17, 2023, 4:35:06 PM7/17/23
to Maria Lathouri, Ggplot2
Hey Maria,

You have one numeric variable and two categorical variables in your dataset, but you are plotting y = Temp and x = mean????

As far as I am aware, You should use barplot with categorical variable mainly for displaying their frequency.

Any way, for log scale transformation you have three options (functions).
 the scale_*_log() , scale_*_log10() and scale_*_sqrt().
You just add like you add the layers in ggplot().
* - placerholder for the aesthetic to be modified. X or y in this case.

But You could also scale values without using the log scale transformation, just with scale() function.
Just mutate(values = scale(values)).

I hope this helps.
Good luck

Osório 


Konstantinos Vlachopoulos

unread,
Jul 17, 2023, 4:35:06 PM7/17/23
to Maria Lathouri, Ggplot2
Dear Maria, 

The log transformation of a close to zero decimal yields a negative infinite number, such as log_base10(0.6)=-0.51.
If your experimental design does not have a major flaw and you can comprehend the results, the square root transformation provides an alternative solution. 

The problem rises due to the huge variation that you have in your dataset.

I tried the following code and it yields to a decent graph. 

ggplot(fc, aes(x = Temp, y = Mean, fill = Glass)) + geom_bar(stat = "identity", position = "dodge", aes(y=sqrt(Mean))) + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.position = c(0.45, 0.85), legend.title = element_blank()) + scale_fill_brewer(palette = "Dark2") + scale_color_brewer(palette = "Dark2”)


Hope this helps, 

K

PastedGraphic-1.tiff

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/511435038.1260853.1689543020404%40mail.yahoo.com.
<1689542973237blob.jpg>

Konstantinos Vlachopoulos
Environmental Scientist, MSc Environmental Management
PhD Student in Population Ecology 

Laboratory of Ecosystem Management and Biodiversity
Department of Agriculture Crop Production and Rural Environment
University of Thessaly
τηλ: 6946054670



unnamed.jpg

Maria Lathouri

unread,
Jul 20, 2023, 4:12:36 AM7/20/23
to Konstantinos Vlachopoulos, Ggplot2
Hi Konstantinos, 

no, sqrt transformation doesn't alter the results; actually when you think about it, transforming values to follow a normal distribution before any statistical test is usually a general rule for a start

but also the following for visualisation purposes works also 

breaks <- unique(c(seq(0,1,by = 10), seq(2,10, 2), seq(20,100, 20), 
                seq(100, 400, 100)))
ggplot () + scale_y_continuous(trans = pseudo_log_trans(base = 10), breaks=breaks)

Kind regards,
Maria

Στις Δευτέρα 17 Ιουλίου 2023 στις 09:35:07 μ.μ. GMT+1, ο χρήστης Konstantinos Vlachopoulos <kostisvla...@gmail.com> έγραψε:





Dear Maria, 

The log transformation of a close to zero decimal yields a negative infinite number, such as log_base10(0.6)=-0.51.
If your experimental design does not have a major flaw and you can comprehend the results, the square root transformation provides an alternative solution. 

The problem rises due to the huge variation that you have in your dataset.

I tried the following code and it yields to a decent graph. 

ggplot(fc, aes(x = Temp, y = Mean, fill = Glass)) + geom_bar(stat = "identity", position = "dodge", aes(y=sqrt(Mean))) + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.position = c(0.45, 0.85), legend.title = element_blank()) + scale_fill_brewer(palette = "Dark2") + scale_color_brewer(palette = "Dark2”)


Hope this helps, 

K




To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/2B098F2D-2B10-4827-8FFD-F15DF86122E9%40gmail.com
.

Merve Bilici

unread,
Sep 12, 2023, 10:52:08 AM9/12/23
to Maria Lathouri, Konstantinos Vlachopoulos, Ggplot2
Hi all,
I hope you are well. I have a kind of small question about plotting and would love to hear from you.

I want to create a bar plot using ggplot package with a data set that has triplicate values for each sample side by column in the table (in the same way that prism works). What I mean is that each sample has experimental repeats.
So like x sample has repeated values in columns 1, 2, and 3, and I want to have this represented in a bar (see the pic attached) but how do I tell R to use these three columns for x in the argument, aes? 
Thanks for your suggestions! 
Screen Shot 2023-09-10 at 12.19.57 PM.png


All the best,
Merve


'Maria Lathouri' via ggplot2 <ggp...@googlegroups.com>, 20 Tem 2023 Per, 09:12 tarihinde şunu yazdı:
Reply all
Reply to author
Forward
0 new messages