ggplot2 line plot order - r

I have a series of ordered points as shown below:
However when I try to connect the points by a line, I get the following output:
The plot is connecting 26 to 1 and 25 to 9 and 10 (some of the errors), instead of following the order. The code for plotting the points is given below:
p<-ggplot(aes(x = x, y = y), data = spat_loc)
p<-p + labs(x = "x Coords (Km)", y="Y coords (Km)") +ggtitle("Locations")
p<-p + geom_point(aes(color="Red",size=2)) + geom_text(aes(label = X))
p + theme_bw()
Plotting code:
p +
geom_line((aes(x=x, y=y)),colour="blue") +
theme_bw()
The file which contains the locations have the following structure:
X x y
1 210 200
.
.
.
where X is the numeric ID and x and y are the pair of co-ordinates.
What do I need to do to make the line follow the ordering of points?

geom_path() will join points in the original order, so you can order your data in the way you want it joined, and then just do + geom_path(). Here's some dummy data:
dat <- data.frame(x = sample(1:10), y = sample(1:10), order = sample(1:10))
ggplot(dat[order(dat$order),], aes(x, y)) + geom_point() + geom_text(aes(y = y + 0.25,label = order)) +
geom_path()

Related

R ggplot boxplot: order filling variable

I am generating a number of boxplots, each for a different marker, filled by a categorical variable: 'CR' and 'No CR'.
I want the left box in the plot to be the 'No CR'-fill and the right one 'CR'. Like this one:
However, for some plots, I get it the other way around (left 'CR' and right 'No CR')
How can I control (order?) which filling category is left and which one is right?
Here is part of my code:
head(df)
# ID y CR
# 1 1 0 No CR
# 2 2 0 No CR
# 3 3 0 CR
# 4 4 4 No CR
ggplot(df, aes(x = CR, y = y)) +
geom_boxplot(aes(fill=CR)) +
labs(title="Highly differential peptides") +
scale_fill_manual(values=c("#35978f","#D6604D"))+
stat_compare_means( label.y = maxadn,size=5)
You can relevel your CR variable :
df$CR=factor(df$CR,levels=c("No CR","CR"))
and then try to replot
It's nice to include a minimal, reproducible example to make it easier to answer your question thoroughly. First I'll simulate some data:
library("ggplot2")
df <- data.frame(
CR = sample(c("CR", "No CR"), 20, replace=TRUE),
y = rpois(20, 2)
)
Then, as explained in this question, you can either set the limits directly:
ggplot(df, aes(x = CR, y = y)) +
geom_boxplot(aes(fill=CR)) +
scale_fill_manual(values=c("#35978f","#D6604D")) +
scale_x_discrete(limits=c("No CR", "CR"))
or use factor levels to control the order:
ggplot(df, aes(x = factor(CR, levels=c("No CR", "CR")), y = y)) +
geom_boxplot(aes(fill=CR)) +
scale_fill_manual(values=c("#35978f","#D6604D")) +
labs(x = "CR")
Without any reordering:
ggplot(df, aes(x = CR, y = y)) +
geom_boxplot(aes(fill=CR)) +
scale_fill_manual(values=c("#35978f","#D6604D"))

Trying to plot data from two different datasets with two axes

So I am using chemistry and precipitation data in the following two df:
chem_df
rain_df
I plotted the two datasets using ggplot() and in order to get 2 axes used the sex.axis function of the scale_y_continuous as follows:
chem_rain_fig <- ggplot() +
geom_point(data = chem_df, aes(x = Date, y = Temp)) +
geom_line(data = rain_df, aes(x = Date, y = Rain)) +
scale_y_continuous(name = "Temp", sec.axis = (.~*, name = "Rain"))
But it keeps plotting both of the data sets to the original y-axis as follows:
Graph with Issue
I would like to just note that the rain data is between 0-10 cm, so that is why it follows the first axis and not the secondary axis with the limit(0,10)
This might answer your question
In essence, you have to manually transform your data and scale to make it appear the right size. Can't try without sample data but this should work, multiplying by 2 and dividing the scale:
chem_rain_fig <- ggplot() +
geom_point(data = chem_df, aes(x = Date, y = Temp)) +
geom_line(data = rain_df, aes(x = Date, y = Rain*2)) +
scale_y_continuous(name = "Temp", sec.axis = sec_axis(~./2, name = "Rain"))

ggplot: Plotting y-variable as continuous line with geom_line [duplicate]

I have a series of ordered points as shown below:
However when I try to connect the points by a line, I get the following output:
The plot is connecting 26 to 1 and 25 to 9 and 10 (some of the errors), instead of following the order. The code for plotting the points is given below:
p<-ggplot(aes(x = x, y = y), data = spat_loc)
p<-p + labs(x = "x Coords (Km)", y="Y coords (Km)") +ggtitle("Locations")
p<-p + geom_point(aes(color="Red",size=2)) + geom_text(aes(label = X))
p + theme_bw()
Plotting code:
p +
geom_line((aes(x=x, y=y)),colour="blue") +
theme_bw()
The file which contains the locations have the following structure:
X x y
1 210 200
.
.
.
where X is the numeric ID and x and y are the pair of co-ordinates.
What do I need to do to make the line follow the ordering of points?
geom_path() will join points in the original order, so you can order your data in the way you want it joined, and then just do + geom_path(). Here's some dummy data:
dat <- data.frame(x = sample(1:10), y = sample(1:10), order = sample(1:10))
ggplot(dat[order(dat$order),], aes(x, y)) + geom_point() + geom_text(aes(y = y + 0.25,label = order)) +
geom_path()

In ggplot2, geom_text() labels are misplaced below my data points (as pictured). How to overlay them onto points?

I'm using ggplot2 to create a simple dot plot of -1 to +1 correlation values using the following R code:
ggplot(dataframe, aes(x = exit)) +
geom_point(aes(y= row.names(dataframe))) +
geom_text(aes(y=exit, label=samplesize))
The y-axis has text labels, and I believe those text labels may be the reason that my geom_text() data point labels are squished down into the bottom of the plot as pictured here:
How can I change my plotting so that the data point labels appear on the dots themselves?
I understand that you would like to have the samplesize appear above each data point in the plot. Here is a sample plot with a sample data frame that does this:
EDIT: Per note by Gregor, changed the geom_text() call to utilize aes() when referencing the data. Thanks for the heads up!
top10_rank<-
String Number
4 h 0
1 a 1
11 w 1
3 z 3
7 z 3
2 b 4
8 q 5
6 k 6
9 r 9
5 x 10
10 l 11
x<-ggplot(data=top10_rank, aes(x = Number,
y = String)) + geom_point(size=3) + scale_y_discrete(limits=top10_rank$String)
x + geom_text(data=top10_rank, size=5, color = 'blue',
aes(x = Number,label = Number), hjust=0, vjust=0)
Not sure if this is what you wanted though.
Your problem is simply that you switched the y variables:
# your code
ggplot(dataframe, aes(x = exit)) +
geom_point(aes(y = row.names(dataframe))) + # here y is the row names
geom_text(aes(y =exit, label = samplesize)) # here y is the exit column
Since you want the same y-values for both you can define this in the initial ggplot() call and not worry about repeating it later
# working version
ggplot(dataframe, aes(x = exit, y = row.names(dataframe))) +
geom_point() +
geom_text(aes(label = samplesize))
Using row names is a little fragile, it's a little safer and more robust to actually create a data column with what you want for y values:
# nicer code
dataframe$y = row.names(dataframe)
ggplot(dataframe, aes(x = exit, y = y)) +
geom_point() +
geom_text(aes(label = samplesize))
Having done this, you probably don't want the labels right on top of the points, maybe a little offset would be better:
# best of all?
ggplot(dataframe, aes(x = exit, y = y)) +
geom_point() +
geom_text(aes(x = exit + .05, label = samplesize), vjust = 0)
In the last case, you'll have to play with the adjustment to the x aesthetic, what looks right will depend on the dimensions of your final plot

Grouping labels when x is a factor variable in ggplot2

I'm trying to replace the x-axis labels "A0" and "A1" by one "A" which can be placed in the middle of "A0" and "A1". It would be better if there is a method which works like the following question:
grouping of axis labels ggplot2
By that, I mean to redraw the x-axis only for each group, and leave a blank between groups.
Here is the code I'm working on:
y = 1*round(runif(20)*10,1)
x1 = c("A","B")
x2 = c(0,1)
x = expand.grid(x1,x2)
xy = cbind(x,y)
xy$z = paste(xy$Var1,xy$Var2,sep="")
p <- ggplot(xy, aes(x=factor(z), y=y,fill=factor(Var2)))
p + geom_boxplot() + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C"))
Try this. No need for the variable z, just use position="dodge":
p <- ggplot(xy, aes(x=factor(Var1), y=y,fill=factor(Var2)))
p + geom_boxplot(position="dodge") + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C"))

Resources