Wrong vertex order in geom_line plot - r

Getting a strange ordering of vertices in a geom_line plot. Left hand plot is base R; right is ggplot.
Here's the shapefile I'm working with. This will reproduce the plot:
require(ggplot2); require(maptools)
rail = readShapeLines('railnetworkLine.shp')
rail_dat = fortify(rail[1,])
ggplot(rail_dat) + geom_line(aes(long, lat, group=group)) + coord_equal()
Any idea what is causing this? The data order of fortify seems correct, as plotting separately lines() confirms.

Use geom_path instead of geom_line. geom_line orders the data from lowest to highest x-value (long in this case) before plotting, but geom_path plots the data in the current order of the data frame rows.
ggplot(rail_dat) +
geom_path(aes(long, lat)) + coord_equal()

Related

ggplot2 only plotting axes - not the points

I have a CSV file like:
date;foo
2016-07-01;0,54
2016-08-01;0,54
2016-09-01;0,50
2016-10-01;0,49
but then read into R and plotted
foo2 <- read.csv2("here")
ggplot(foo2, aes(x=date, y=foo))
The output is empty. I.e. axes are present but no points are plotted.
A regular plot(foo2$foo) simply plots the points - what could be wrong here?
You need to add a geom to your plot. If you want a line plot add...
ggplot(foo2, aes(x=date, y=foo)) + geom_line()
If you want a scatter plot...
ggplot(foo2, aes(x=date, y=foo)) + geom_point()
You can find more geoms here.

Drawing flipped Normal distribution in R without using coord_flip()

Good day
Without using coord_flip(), Is there a way to draw normal distribution flipped by exchanging position x and y in aes()?
I' ve tried as below.
df3 <- data.frame(x=seq(-6,6,b=0.1),y=sapply(seq(-6,6,b=0.1),function(x) dnorm(x)))
ggplot(df3,aes(y,x))+ geom_line() # x,y position exchanged
I'm not sure what's wrong with coord_flip, but you can avoid it with geom_path. geom_path connects the points in the order they appear in the data, rather than in order of the magnitude of the x-value. So you just need to make sure the data are ordered by y-axis value (which they already are here).
ggplot(df3, aes(y,x)) +
geom_path() +
theme_classic()

How can I plot several series as lines and one of them as area using ggplot2?

I'm trying to accomplish something that I used to do in Excel, I have several timeseries for the same time interval and would like to plot them as lines (easy enough using ggplot geom_line), but one of them should be plotted as an area plot.
Basically something like this:
Plase note that the series S_1 is plotted as area.
I have already tried adding geom_area() with aes values equal to the value of the area series:
ggplot(df.lines, aes(x=Index, y=Value, colour=Series)) + geom_line() + geom_area(aes(x=df.area$Index, y=df.area$S_1))
How could I acomplish something like this using ggplot2?
Difficult to test with no dataset (can you provide one on the example, you can use dput()), but in geom_area, the selection should be made in the data argument.. like this for instance..
ggplot +
geom_area(data = df.area[df.area$Series == "S_1", ], aes(x=Index, y=Value))
geom_line(data = df.lines, aes(x=Index, y=Value, colour=Series))

Missing scale on ggplot 2

I am creating a graph using ggplot2. Here is the first output of the graph before any tidying is done.
And here is the code:
graph <- ggplot(data = village.times,
aes(x=village.times$a6ncopo, y=(village.times$a5species=="funestus")))
+ geom_bar(stat="identity", position = "stack", fill="#FF4444")
What I don't know is why there isn't a scale on the y axis and how to remove the True-False labels. Is there a way I can force ggplot to include a scale on the y axis or do I have to change the way I use my data?
Maybe subsetting your data frame before using ggplot and just creating a histogram? Otherwise I don't what your expected result should be...
ggplot(subset(village.times, a5species=="funestus"),
aes(x=a6ncopo)) +
geom_bar()

ggplot2: Is there a way to overlay a single plot to all facets in a ggplot

I would like to use ggplot and faceting to construct a series of density plots grouped by a factor. Additionally, I would like to a layer another density plot on each of the facets that is not subject to the constraints imposed by the facet.
For example, the faceted plot would look like this:
require(ggplot2)
ggplot(diamonds, aes(price)) + facet_grid(.~clarity) + geom_density()
and then I would like to have the following single density plot layered on top of each of the facets:
ggplot(diamonds, aes(price)) + geom_density()
Furthermore, is ggplot with faceting the best way to do this, or is there a preferred method?
One way to achieve this would be to make new data frame diamonds2 that contains just column price and then two geom_density() calls - one which will use original diamonds and second that uses diamonds2. As in diamonds2 there will be no column clarity all values will be used in all facets.
diamonds2<-diamonds["price"]
ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) +
geom_density(data=diamonds2,aes(price),colour="blue")
UPDATE - as suggested by #BrianDiggs the same result can be achieved without making new data frame but transforming it inside the geom_density().
ggplot(diamonds, aes(price)) + geom_density()+facet_grid(.~clarity) +
geom_density(data=transform(diamonds, clarity=NULL),aes(price),colour="blue")
Another approach would be to plot data without faceting. Add two calls to geom_density() - in one add aes(color=clarity) to have density lines in different colors for each level of clarity and leave empty second geom_density() - that will add overall black density line.
ggplot(diamonds,aes(price))+geom_density(aes(color=clarity))+geom_density()

Resources