Change y-axis to a logarithmic scale in Base R

I have the following CSV data showing the mean and standard error of ammonia concentrations for 10 sites.

 Site, Mean, StdEr
CountyUS_Wet, 0.01, 0.00  
CountyUS_Dry, 0.23, 0.10
CountyDS_Wet,   0.01,  0.00    
CountyDS_Dry,  0.12,  0.05  
CountyOF_Wet, 0.11,  0.10
CountyOF_Dry, 0.25, 0.13
CastleOF_Wet, 0.14, 0.12
CastleOF_Dry, 0.86, 0.61
CourtOF_Wet, 0.06, 0.05
CourtOF_Dry, 0.49, 0.15

I uploaded this data into R and and created a barplot using the code:

windowsFonts(A = windowsFont("Times New Roman"))
par(family = "serif")

NH4plot <- barplot(data.dat$NH4Mean, ylim = c(0,1.6), family = "A", xlab = "Sample Location", ylab = "NH4 Concentration (mg/L)", col = c('Grey', 'White', 'Grey', 'White', 'Grey', 'White', 'Grey', 'White', 'Grey', 'White'))
axis(1, at = c(1.3, 3.7, 7.3, 10.8, 14.5), labels = c("BO1 US", "BO1 DS", "BO1", "BO2", "BO3"), family = "A")
arrows(x0 = NH4plot,
       y0 = data.dat$NH4Mean + data.dat$NH4StEr,
       y1 = data.dat$NH4Mean - data.dat$NH4StEr,
       angle = 90,
       code = 3, 
       length = 0.1)
legend(1, 1.5, legend = c("Wet Weather", "Dry Weather"), fill = c("Grey", "White"), bty = "n")

This code produced the following plot:
image

As shown in the plot, there is a large variation in the NH4 concentrations among sites and a logarithmic scale would better represent the low concentrations shown in some of the bars.

I am currently trying to change the scale of the y-axis to be logarithmic, but not having much success. Does anyone have any advice on how to show my y-axis on a logarithmic scale? TIA

Try par(ylog = TRUE).

I just tried that and it didn't work. Do I include this in the barplot() function or as a separate line of code?

Separate line of code before barplot(). ... I think.

Something like this seems to work,

barplot( dat1$Mean,  log = "y")

Data

dat1  <- structure(list(Site = c("CountyUS_Wet", "CountyUS_Dry", "CountyDS_Wet", 
        "CountyDS_Dry", "CountyOF_Wet", "CountyOF_Dry", "CastleOF_Wet", 
        "CastleOF_Dry", "CourtOF_Wet", "CourtOF_Dry"), Mean = c(0.01, 
             0.23, 0.01, 0.12, 0.11, 0.25, 0.14, 0.86, 0.06, 0.49), StdEr = c(0, 
             0.1, 0, 0.05, 0.1, 0.13, 0.12, 0.61, 0.05, 0.15)), row.names = c(NA, 
            -10L), class = "data.frame")
1 Like

Better than my suggestion.

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.