How to connect lines when x are strings? [duplicate] - r

This question already has answers here:
ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"
(6 answers)
Closed 12 months ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
library(ggplot2)
x=letters[1:3]
y=1:3
qplot(x, y)
qplot(x, y, geom=c('point', 'line'))
geom_path: Each group consists of only one observation. Do you need to adjust
the group aesthetic?
I want to connect lines between the points. But when the x is a string, the above commands won't work. It works when the x is numeric. I'd think qplot should be made more user-friendly in this case.
How to make it connect the points with lines when x is a string?

One solution is provided by #stefan. Another one could be the following.
Sample data:
x=letters[1:3]
y=1:3
Sample code:
d <- data.frame(x, y) %>%
mutate(x = x %>%
factor(levels = x))
library(ggplot2)
ggplot(data = d, aes(x = x, y = y, group = 1)) +
geom_line() +
scale_x_discrete(labels = x, breaks = x)
Plot:

Related

Adding legend to ggplot curves plotted on the same axis [duplicate]

This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 4 months ago.
I have a graph that I'm trying to add a legend to but I can't find any answers.
Here's what the graph looks like
I made a dataframe containing my x-axis as a colum and several othe columns containing y values that I graphed against x (fixed) in order to get these curves. I want a legend to appear on the side saying column 1, ...column 11 and corresponding to the color of the graph
How do I do this? I feel like I'm missing something obvious
Here's what my code looks like:(sorry for the pic. I keep getting errors that my code is not formatted correctly even though I'm using the code button)
interval is just 2:100 and aaaa etc... is a vector the same length as interval.
As Peter says, you will need to convert your data into "long" format. Here is an example using reshape2::melt:
library(reshape2)
library(ggplot2)
n <- 20
df <- data.frame(x = seq(n))
tmp <- as.data.frame(do.call("cbind", lapply(seq(5), FUN = function(x){rnorm(n)})))
names(tmp) <- paste0("aaaa", letters[1:5])
df <- cbind(df, tmp)
head(df)
df2 <- melt(df, id.vars = "x")
head(df2)
ggplot(data = df2) + aes(x = x, y = value, color = variable) +
geom_point() +
geom_line()

ggplot - Plotting several sin functions on one graph [duplicate]

This question already has answers here:
Plot two graphs in same plot in R
(17 answers)
Plotting two variables as lines using ggplot2 on the same graph
(5 answers)
Closed 3 years ago.
I want to show graphically how the summation of two different sin curves looks like.
So, I am trying to make a single graph that shows two different sin function and their sum. So, three curves on one graph.
How can I do it with ggplot layers?
I am defing two sin functions (y and z)
x <- seq(0, 16*pi, 0.01)
y <- 2*sin(3*(x-1))
z <- sin(x)
summing up the two curves:
t <- y + z
I can see the three separately with:
plot(x,y,type="l")
plot(x,z,type="l")
plot(x,t,type="l")
But how can I plot the three functions?
I tried this but it does not work
ggplot(x,
qplot(y,x,geom="path", xlab="time", ylab="Sine wave") +
qplot(z,x,geom="path", xlab="time", ylab="Sine wave"))
Store everything in a data.frame, reshape from wide to long, and plot:
library(tidyverse)
data.frame(x = x, y = y, z = z, t = y + z) %>%
pivot_longer(-x) %>%
ggplot(aes(x, value, colour = name)) +
geom_line()
Sample data
x <- seq(0, 16*pi, 0.01)
y <- 2*sin(3*(x-1))
z <- sin(x)

How to remove dots and extend boxplots in ggplot2 [duplicate]

This question already has answers here:
ggplot2 - Boxplot Whiskers at Min/Max
(2 answers)
Closed 7 years ago.
I have some data that I'm trying to build some boxplots with, but I'm getting this error:
Warning message: Removed 1631 rows containing non-finite values
(stat_boxplot).
There are no NA values and all the data seems fine. How can I fix this as these are certainly valuable points in my data and should be extended by the whiskers?
Data
The data is fairly large, and I couldn't get a smaller subsample to produce the errors, so I'll just post the original data.
dat.rds
ggplot2
dat <- readRDS("./dat.rds")
ggplot(dat, aes(x = factor(year), y = dev)) + geom_boxplot() + ylim(-40, 260)
Edit
I was able to get it to work in boxplot with `range = 6'. Is there a way to do this in ggplot?
boxplot(dev~year, data = d, range = 6)
Remove the ylim restriction and use the coef argument of geom_boxplot, then it works fine:
library(ggplot2)
download.file(url = "https://www.dropbox.com/s/5mgogyclhim6hom/dat.rds?dl=1", tf <- tempfile(fileext = ".rds"))
dat <- readRDS(tf)
ggplot(dat, aes(x = factor(year), y = dev)) +
geom_boxplot(coef = 6)

Plotting multiple columns with ggplot2 [duplicate]

This question already has answers here:
Plot multiple columns on the same graph in R [duplicate]
(4 answers)
Closed 4 years ago.
I need to plot the following dataset in the same graph.
Bin1,Bin2,Bin3,Cat
4,3,5,S
6,4,5,M
3,5,4,M
1,4,5,M
,5, ,M
In each bin, first data point belongs to a different category than the rest. (So I added the Cat column)
I need to plot these as points (different colors for the different categories)
Following lines of code achieve what I need for a single bin
p <- ggplot(data,aes(Bin1,1))
p + geom_point(aes(color=Cat, size=Cat))
How do I do this for the entire dataset ?
Here is a related question?
What if I need to use a bunch of columns to color the points. Color Bin1 points according to Cat1 and so on..
Bin1,Cat1,Bin2,Cat2
4,S,5,S
6,L,5,M
3,M,4,L
1,M,5,L
3,M
How do I do this??
library(reshape2)
library(ggplot2)
ggplot(melt(df, id.vars = "Cat"), aes(value, variable, colour = Cat)) +
geom_point(size = 4)
Just melt the data.frame and plot it.
library(reshape2)
dataM <- melt(data, id.vars = "Cat")
p <- ggplot(dataM, aes(value, variable, colour = Cat, size = Cat) + geom_point()

Month-Year bar graph plot faceted and filled on year; with data input as date in char format

Environment: Win 7 HP, R v2.15.1
What I wish to get to:
Plot y (numeric) vs x (date) with
labels month-year abbrev, sorted mon+year, las2 vertically aligned
colours filled by year
facet-grid by year
I have tried different approaches after reading up different threads in this forum, but unable to get what I need. Need help. Attaching sample data and results.
MySample Data
x <- c("04-01-10","05-01-10","06-01-10","07-01-10","08-01-10","09-01-10","10-01-10","11-01-10","12-01-10","01-01-11","02-01-11","03-01-11","04-01-11","05-01-11","06-01-11","07-01-11","08-01-11","09-01-11","10-01-11","11-01-11","12-01-11","01-01-12","02-01-12","03-01-12","04-01-12","05-01-12","06-01-12")
y <- c(120,210,130,160,190,210,80,70,110,120,140,160,130,200,110,180,210,200,90,60,100,100,120,170,100,180,120)
x is date (character) in mm-dd-yy format tz:IST (Calcutta / Asia)
data has only single y value per month which is on the start date of the month
Convert to Data Frame
MySample <- data.frame(x) ## convert to dataframe
MySample$y <- y
load required libraries
require(lubridate)
require(ggplot2)
MySample Base Plot
1) Plot x vs y
ggplot(MySample, aes(MySample$x, MySample$y)) +
geom_bar(y=MySample$y,stat="identity")
Gave me base plot results
2) Plot x vs y + fill=year
ggplot(MySample, aes(MySample$x, MySample$y, fill=year(MySample$x))) +
geom_bar(y=MySample$y,stat="identity")
gave me fills but have 5 fill years with 2010,2010.5,2011,2011.5,2012
I have tried different approaches but running into one error or another.
3) Plot x vs y + fill=year + facet_grid(year)
ggplot(MySample, aes(x, y, fill=year(x))) +
geom_bar(y=MySample$y,stat="identity") +
facet_grid(. ~ year(MySample$x))
Get : Error in layout_base(data, cols, drop = drop) :
At least one layer must contain all variables used for facetting
4) Plot x vs y + fill=year + facet_grid(year) + labels-month (abbr)
ggplot(MySample, aes(x, y, fill=year(x))) +
geom_bar(y=MySample$y,stat="identity") +
scale_x_date(labels=month(MySample$x,label=TRUE,abbr=TRUE))
Get : Error in scale_labels.continuous(scale, major) : Breaks and labels are different lengths
I'm stuck and need help to move forward.
Need solutions to address the following requirements:
3 fill years only - 2010,2011,2012
xlabels - %b%y format; sorted on month-year sequence; las2 positioned (vertical)
facet_grid by year with only that year's xlabels and bars in the appropriate facet-grid
To answer your 3 points:
use scale_fill_gradient(breaks=unique(MySample$year))
use the standard built-in date system instead of lubridate. you can specify the format in ggplot2 with date_format.
use: +facet_grid(. ~ year, scales = "free")
The following code will do it:
MySample$date <- as.Date(MySample$x, "%m-%d-%y")
MySample$year <- year(MySample$date)
ggplot(MySample, aes(date, y, fill = year)) +
geom_bar(y=y,stat="identity") +
facet_grid(. ~ year, scales = "free") +
scale_x_date(labels = date_format("%b/%y")) +
scale_fill_gradient(breaks=unique(MySample$year))

Resources