Histogram not displaying properly - r

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().

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

How to determine x, y coordinates when adding text in metafor

I’m using the metafor package to create Forest plots in R. I’d like to add text to my plots to create labels using the text() function. I’m wondering what the simplest way is to determine the x,y coordinates of where I want my text to go. Currently I just guess and see how it looks and then edit as necessary. Is there a way to overlay a grid over my plot or something to guide me (and then remove it after)?
Thank you!
Start by saving what forest() returns:
x <- forest(res)
And then take a look at x. Among other things, it contains xlim and ylim, which are the x-axis and y-axis limits. Try this:
abline(v=x$xlim)
abline(h=x$ylim)
Also useful:
text(x$xlim[1], x$rows, x$rows, xpd=NA)
I hope this helps.

Failure of producing a histogram in R if using breaks=a numeric vector

Is there any difference between using breaks=c(1,2,3,4,5) and breaks=c(1:5)?
I am using hist(...) function to get a histogram for a dataset.
If I use hist(MyDataFile$V3, breaks=c(1,2,3,4,5)), then I get the correct histogram.
If I use hist(MyDataFile$V3, breaks=c(1:5)) or
hist(MyDataFile$V3, breaks=seq(1,5,1)), then the histogram does not show correct rectangles, and it only gives me a lot of vertical parallel lines at some breaks instead, but not the correct histogram.
I cannot figure out what is wrong with my the value of my breaks.
[UPDATA] It is a silly mistake. The problem is solved if set freq=FALSE. Thank you all for your time.

Aesthetics for graphs in R WITHOUT using ggplot

I looked for the easthetics to make the histogram look better in my R code, nonetheless I was wondering if I could change the size of the bins or maybe the margin or put a color background in the plot WITHOUT using ggplot.
Thank you.

Hiding Data Point on qplot

I have downloaded the directlabels package in R to further enhance the ggplot2 experience, however I would like to delete the data point of a scatterplot once I have added the labels to them. Is there any way to hide these? My code goes something like this:
q<-qplot(x,y)+geom_point(aes(colour=z))
direct.label(q,list(cex=0.75,fontface="bold",bumpup))
But I'm not sure where the command to hide the data point would be. I would use first.qp but in this case I get the error
Error in order.labels(d) : labels are not aligned
so is there a better way of doing this?
You example is not reproducible. So I will just answer this question:
"I would like to delete the data point of a scatterplot once I
have added the labels to them."
You can easily for example remove a layer from the gg object. First I create a ggplot2 example and I decorate it using direct.label.
library(directlabels)
scatter <- qplot(jitter(hwy),jitter(cty),data=mpg,colour=class,
main="Fuel efficiency depends on car size")
scatter <- direct.label(scatter,list(cex=0.7,bumpup))
I remaove the first layer now(the geom_point layer)
scatter$layers[1] <- NULL
Then you get this plot , as you see I had only labels without points:
scatter

Resources