How to make an R barplot with a log y-axis scale? - r

This should be a simple question... I'm just trying to make a barplot from a vector in R, but want the values to be shown on a log scale, with y-axis tick marks and labelling. I can make the normal barplot just fine, but when I try to use log or labelling, things go south.
Here is my current code:
samples <- c(10,2,5,1,2,2,10,20,150,23,250,2,1,500)
barplot(samples)
Ok, this works. Then I try to use the log="" function defined in the barplot manual, and it never works. Here are some stupid attempts I have tried:
barplot(samples, log="yes")
barplot(samples, log="TRUE")
barplot(log=samples)
Can someone please help me out here? Also, the labelling would be great too. Thanks!

The log argument wants a one- or two-character string specifying which axes should be logarithmic. No, it doesn't make any sense for the x-axis of a barplot to be logarithmic, but this is a generic mechanism used by all of "base" graphics - see ?plot.default for details.
So what you want is
barplot(samples, log="y")
I can't help you with tick marks and labeling, I'm afraid, I threw over base graphics for ggplot years ago and never looked back.

This should get your started fiddling around with ggplot2.
d<-data.frame(samples)
ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) +
geom_bar(stat="identity") +
scale_y_log10()
Within the scale_y_log10() function you can define breaks, labels, and more. Similarly, you can label the x-axis. For example
ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) +
geom_bar(stat="identity") +
scale_y_log10(breaks=c(1,5,10,50,100,500,1000),
labels=c(rep("label",7))) +
scale_x_discrete(labels=samples)

Related

How to use the x-axis to order the function

Hi I am using ggplot2 to create a scatter graph. For some reason it keeps using the y-axis variables to order my scatter graph. I'm new to this so I barely know what I'm doing, so if someoen could help, would be much appreciated!
What I've noticed is that my Rstudio orders the numbers going in order like 1,10,11,12...2,21,22,23 instead of going the standard 1,2,3,4,5,6,7
Diesel_Prices_Jan19 <- ggplot(Prices_4, aes(V4, V1))
Diesel_Prices_Jan19 + geom_point()
As a tip its probably best to give a good reprex. Check the type of that column and maybe try turning it into a factor

Unable to figure out why the scale_x_continuous function in ggplot is not adding tick marks

Sorry for the newbie question, but I can't find the answer to this anywhere using a Google search. I am working through a beginner R open course and the directions were to create the below plot using ggplot2 and mark the x-axis every 2 units. I am using the scale_x_continuous function and using the same format as I see in examples online. However, the below code actually removes all the tick marks from the x-axis except for the limits. Thanks in advance for any help!
library(ggplot2)
ggplot(diamonds, aes(price, depth)) +
geom_point() +
scale_x_continuous(breaks = c(0,19000,2))

Histogram not displaying properly

I am working on a diametric class distribution data, with over 10,000 individuals, and I wanted to make a histogram to show the results, but I am having a problem when I try to plot it.
When I tried using the histogram function
histogram(data)
I get a histogram that is calculating the percentage of individuals
Histogram using the histogram function
Then I tried using qplot function
qplot(data, geom = "histogram")
And got a histogram like the one you can see in image 2
qplot function
So I thought maybe is a problem with the y-axis scale so I tried using ylim() and the plot I got was only the axis with no data
Could someone please tell me what I'm doing wrong?
I'm trying to get a histogram like the one in image 3Histogram goal
I really appreciate your help to point out if I am not adding other codes or maybe redirect me to a cheat sheet that can clarify some things
thanks
Try to use the ggplot2 package, for example:
library(ggplot2)
ggplot(data = [YOUR_DATA], aes([YOUR_VARIABLE])) +
geom_histogram()
EDIT:
Looking at your desired result, I think you want to use barplot() instead of histogram().

R ggplot2 scales cancel each other

I'm having some trouble plotting with ggplot2. When trying to use 2 different scale functions, they won't act at the same time, that is, only one command will actually work, depending on the order. For example, if I do plot + scale_x_discrete(...) and then plot + scale_fill_discrete(...), only the later will work (editing the legend), while the other wont, leaving the x axis unedited. If I switch the order of commands, then the axis is edited, while the legend is neglected.
Could you please explain why this is happening and how I should be doing this?
Did you try
plot + scale_x_discrete(...) + scale_fill_discrete(...)

Indicating the statistically significant difference in bar graph USING R

This is a repeat of a question originally asked here: Indicating the statistically significant difference in bar graph but asked for R instead of python.
My question is very simple. I want to produce barplots in R, using ggplot2 if possible, with an indication of significant difference between the different bars, e.g. produce something like this. I have had a search around but can't find another question asking exactly the same thing.
I know that this is an old question and the answer by Didzis Elferts already provides one solution for the problem. But I recently created a ggplot-extension that simplifies the whole process of adding significance bars: ggsignif
Instead of tediously adding the geom_path and annotate to your plot you just add a single layer geom_signif:
library(ggplot2)
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
geom_signif(comparisons = list(c("versicolor", "virginica")),
map_signif_level=TRUE)
Full documentation of the package is available at CRAN.
You can use geom_path() and annotate() to get similar result. For this example you have to determine suitable position yourself. In geom_path() four numbers are provided to get those small ticks for connecting lines.
df<-data.frame(group=c("A","B","C","D"),numb=c(12,24,36,48))
g<-ggplot(df,aes(group,numb))+geom_bar(stat="identity")
g+geom_path(x=c(1,1,2,2),y=c(25,26,26,25))+
geom_path(x=c(2,2,3,3),y=c(37,38,38,37))+
geom_path(x=c(3,3,4,4),y=c(49,50,50,49))+
annotate("text",x=1.5,y=27,label="p=0.012")+
annotate("text",x=2.5,y=39,label="p<0.0001")+
annotate("text",x=3.5,y=51,label="p<0.0001")
I used the suggested method from above, but I found the annotate function easier for making lines than the geom_path function. Just use "segment" instead of "text". You have to break things up by segment and define starting and ending x and y values for each line segment.
example for making 3 lines segments:
annotate("segment", x=c(1,1,2),xend=c(1,2,2), y= c(125,130,130), yend=c(130,130,125))

Resources