Set limits in ggplot without loosing data points [duplicate] - r

This question already has an answer here:
Limit ggplot2 axes without removing data (outside limits): zoom
(1 answer)
Closed 1 year ago.
I want to focus on certain numbers above 0 in the y axis, so I tried to use
ylim(ymin=0,ymax=15)
but by doing this I removed values that are not in the figure
I tried to find another argument that will only "zoom" the figure but do not remove any actual value and the line will remain
Does anyone know any argument that will be valid?
E.g. figures

Try to use this instead of ylim()
coord_cartesian(ylim=c(0, 15))

Related

ggplot2 geom_col() does not show all items [duplicate]

This question already has answers here:
order of stacked bars in ggplot
(1 answer)
Force ggplot legend to show all categories when no values are present [duplicate]
(1 answer)
Can you put labels between horizontal bars?
(2 answers)
Closed last month.
I'm new to R and I'm trying to plot a small survey where I used a likert-scale.
I'm using
ggplot(Likert, aes(x=Anzahl, y=Frage, fill=Zustimmung)) + geom_col()
to display the data. There are probably better ways to do this and I probably should use data frames as I currently don't use them.
Now the actual Problem: The variable "Zustimung" has 5 different specifications but only 4 of them are shown and there are sorted weirdly.
I've found a few different posts online which seem to be regarding this issue but I can't understand them. Could someone please explain why this problem occures and how I can fix this?

How to display truncated error bars with ggplot? [duplicate]

This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 9 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example

color in ggplot2 legend not matching figure [duplicate]

This question already has answers here:
Why are the colors wrong on this ggplot? [duplicate]
(2 answers)
GGPLOT Color won't work [closed]
(1 answer)
Closed 4 years ago.
I have a problem getting the legend in ggplot2 to correspond to the color in the figure. And I have extra variables in the legend. I have read many posts on similar issues but unfortunately I have not been able to apply those to this situation - successfully at least. I tried using "fill" and "scale_fill_manual()", "theme()" and several others, but to no avail yet. Hopefully I am just missing something simple here, I appreciate any suggestions!
I am making a map and my code right now is:
ggplot()+ geom_map(data=world, map=world,aes(x=long, y=lat, map_id=region),color="white", fill="#7f7f7f", size=0.05)+
geom_point(aes(x=long.a,y=lat.a, size=var.p, color="red", alpha=0.5))
The plot looks like this:

Change month order using R and ggplot2 [duplicate]

This question already has answers here:
How do you specifically order ggplot2 x axis instead of alphabetical order? [duplicate]
(2 answers)
Closed 4 months ago.
So I'm having a pretty big problem. I'm trying to plot animal density (using two methods) against months.
When I plot one of the two methods, the months go out of order and I can't seem to get them to work.
In the end I want to have each of the two methods (camera.estimations and scat.estimations) on the same plot, with SD of each included and months in the right order. Any pointers from you lovely R folk would be awesome!
Here's my data:
https://docs.google.com/spreadsheet/ccc?key=0AjSTnjcF57_IdEhUXzFyQmtZaG5NXzdzTXZkbFdweXc&usp=sharing
I have used this post to help structure my ggplots before
How do you specifically order ggplot2 x axis instead of alphabetical order?
Manually setting the order of factors works but can be annoying, but is the best answer I have for you :)
without more information hope it helps!

Rescaling the y axis in bar plot causes bars to disappear : R ggplot2 [duplicate]

This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 9 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example

Resources