I am trying to plot a line graph along with points for a data set.
Unfortunately I am unable to plot the line in the graph, however, the points are plotted?
I am getting the error as geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
can anyone help me out?
I have already made sure that all the variables are either numeric or character, and not factors. I also tried to label them as groups but it did not work.
df<- c('a','b','c','d','e')
df1<-1:5
df2<-11:15
df3<-21:25
df4<-cbind(df,df1,df2,df3)
colnames(df4)<-c("Names", "P1","P2","P3")
df4<-as.data.frame(df4)
dfplot <- gather(df4,key="Period", value="Price",-Names,P1,P2,P3 )
dfplot<-dfplot[order(dfplot$Names),]
vars<-c("Price")
vars1<-c("Names","Period")
dfplot[vars] <- sapply(dfplot[vars], as.numeric)
dfplot[vars1]<-sapply(dfplot[vars1], as.character)
ggplot(dfplot, aes(x = Period, y = Price, color = Names),group=5 ) + geom_line()+geom_point()
As your data is grouped, you need to tell the geom_line() which field the data is grouped on:
ggplot(dfplot, aes(x = Period, y = Price, color = Names),group=5 ) + geom_line(aes(group=Names))+geom_point()
Some more information
Related
I have a data list with several columns: selected, total, location. Location is plotted on the x-axis and selected on the y axis. However, I am looking to filter out the values that have totals of less than 10 for selected.
So far, for the plot I have I'm using:
ggplot(rawdata) +
geom_point(aes(x=location, y=selected)) +
geom_smooth(aes(x=location, y=selected, se=F)) +
labs(
title="All bins, selected",
x="location",
y="selected")
Is there a way to do that in ggplot2? I'm assuming we'd use an if statement to include the third column but I'm not even sure if what I'm asking is possible.
You could use filter() when calling your dataset in the ggplot data argument. In the example below, I've filtered out values of 'cty' that are less than ten.
library(tidyverse)
ggplot(data = filter(mpg, cty >= 10), aes(x = hwy, y = cty)) +
geom_point()
I have a dataset where one of the columns is dates but in character format. I used the following code to convert it to dates format and then take the month only:
library(lubridate)
dates <- dmy(Austria$date)
Month <- month(dates, label = TRUE, abbr = FALSE)
The problem is that I am taking levels back for the months which I don't want to. I searched on how to remove the levels but everything I found was about removing levels that are unused (which is not my case).
I also, used the as,Date but I am still having the same problem:
dates_Austria <- as.Date(Austria$date, "%d/%m/%Y")
My final purpose is to make a plot which will have unemployment on the horizontal axis, income level on the vertical axis and then change the color of the plot according to the month, like that:
ggplot(data = my_data, aes(x = unemployment, y = income, colour = Month)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
But by using that code I am getting back different regression lines according to the month. I want one line for all the data and the the rest of the dots of the scatter plot to change colour according to the month.
Any help would be appreciated.
I have a simple plot of same data from an experiment.
plot(x=sample95$PositionA, y=sample95$AbsA, xlab=expression(position (mm)), ylab=expression(A[260]), type='l')
I would like to shade a particular area under the line, let's say from 35-45mm. From what I've searched so far, I think I need to use the polygon function, but I'm unsure how to assign vertices from a big dataset like this. Every example I've seen so far uses a normal curve.
Any help is appreciated, I am very new to R/RStudio!
Here is a solution using tidyverse tools including ggplot2. I use the built in airquality dataset as an example.
This first part is just to put the data in a format that we can plot by combining the month and the day into a single date. You can just substitute date for PositionA in your data.
library(tidyverse)
df <- airquality %>%
as_tibble() %>%
magrittr::set_colnames(str_to_lower(colnames(.))) %>%
mutate(date = as.Date(str_c("1973-", month, "-", day)))
This is the plot code. In ggplot2, we start with the function ggplot() and add geom functions to it with + to create the plot in layers.
The first function, geom_line, joins up all observations in the order that they appear based on the x variable, so it makes the line that we see. Each geom needs a particular mapping to an aesthetic, so here we want date on the x axis and temp on the y axis, so we write aes(x = date, y = temp).
The second function, geom_ribbon, is designed to plot bands at particular x values between a ymax and a ymin. This lets us shade the area underneath the line by choosing a constant ymin = 55 (a value lower than the minimum temperature) and setting ymax = temp.
We shade a specific part of the chart by specifying the data argument. Normally geom functions act on the dataset inherited from ggplot(), but you can override them by specifying individually. Here we use filter to only plot the points where the date is in June in geom_ribbon.
ggplot(df) +
geom_line(aes(x = date, y = temp)) +
geom_ribbon(
data = filter(df, date < as.Date("1973-07-01") & date > as.Date("1973-06-01")),
mapping = aes(x = date, ymax = temp, ymin = 55)
)
This gives the chart below:
Created on 2018-02-20 by the reprex package (v0.2.0).
I have produced a line graph using ggplot. The data contains two groups with 9 samples each that were followed up over 11 time points (x-values). Now, I have tried to give each sample line of one group an individual colour, while giving only a single colour to the samples of the other group (here: black).
Here is the important part of my script.
data <- read.csv2("140929 example.csv",check.names = FALSE)
library(reshape2)
data.m <- melt(data)
library(ggplot2)
ggplot(data.m, aes(x = variable, y = value, group = Group,colour = Group))+
geom_line()+
theme_bw()
This turns out a graph with individual colours for all lines.
How can I improve? Thank you for your help.
This is a bit hard to tell without data or a picture of your current plot. But you can try assigning a new variable to your data.m to control color. I.E. set a new variable up as a sequence then for the solid color group set it up to be the same throughout that group.
data.m$mycolor <- 1:nrow(data.m)
data.m[data.m$group == somegroup,]$mycolor <- 0
Then in your aesthetic use colour = mycolor
I am trying to do the comparison of my observed and modeled data sets for two stations. One station is called station "red" and another is called "blue". I was able to create the facets but when I tried to add two series in one facet, only one facet got updated while other didn't.
This means for blue only one series is plotted and for red two series are plotted.
The code I used is as follows:
# install.packages("RCurl", dependencies = TRUE)
require(RCurl)
out <- postForm("https://dl.dropbox.com/s/ainioj2nn47sis4/watersurf1.csv?dl=1", format="csv")
watersurf <- read.csv(textConnection(out))
watersurf[1:100,]
watersurf$coupleid <- factor(rep(unlist(by(watersurf$id,watersurf$group1,
function(x) {ave(as.numeric(unique(x)),FUN=seq_along)}
)),each=6239))
p <- ggplot(data=watersurf,aes(x=time,y=data,group=id))+geom_line(aes(linetype=group1),size=1)+facet_wrap(~coupleid)
p
Is it also possible to add a third series in the graph but of unequal length (i.e not same interval)?
The output is
I followed the example on this page to create the graphs.
http://www.ats.ucla.edu/stat/r/faq/growth.htm
Is this what you are looking for,
ggplot(data = watersurf, aes( x = time, y = data))
+ geom_line(aes(linetype = group1, colour = group1), size = 0.2)
+ facet_wrap(~ id)