ggplot: Plotting y-variable as continuous line with geom_line [duplicate] - 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

How to separately scale geoms with ggplot?

I am attempting to combine these 2 plots into 1 but I can not figure out how to scale them separately when they are combined. I have scaled the second y axis to the proper level, but it does not adjust the data output on the plot.
The problem is the values of plot 1 range between 0:2 while the range of plot 2 is 0:150000 so even though I have scaled the second y axis, the data will not adjust to that scale. I have read that scale_y_continuous() only adjusts the axis and has no representation of the actual data.
So how would I separately scale each geom?
SETUP:
library(quantmod)
library(tidyverse)
SPY520 <- getOptionChain("SPY", "2022-05-20")
SPY1Mputs <- SPY520[["puts"]]
Plot 1:
Plot 1
SPY1Mputs %>%
ggplot() +
geom_point(aes(x = Strike, y = IV)) +
geom_smooth(aes(x = Strike, y = IV))
Plot 2:
Plot 2
SPY1Mputs %>%
ggplot() +
geom_col(aes(x = Strike, y = OI))
Attempted combine:
Plot 3
SPY1Mputs %>%
ggplot(aes(x = Strike)) +
geom_col(aes(y = OI)) +
geom_point(aes(y = IV)) +
geom_smooth(aes(y = IV)) +
scale_y_continuous(sec.axis = sec_axis((~./60000), name = "IV"))

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"))

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

ggplot2 line plot order

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

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