Apply colours to a line graph using ggplot - r

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

Related

Select data and name when pointing it chart with ggplotly

I did everything in ggplot, and it was everything working well. Now I need it to show data when I point a datapoint. In this example, the model (to identify point), and the disp and wt ( data in axis).
For this I added the shape (same shape, I do not actually want different shapes) to model data. and asked ggplot not to show shape in legend. Then I convert to plotly. I succeeded in showing the data when I point the circles, but now I am having problems with the legend showing colors and shapes separated with a comma...
I did not wanted to make it again from scrach in plotly as I have no experience in plotly, and this is part of a much larger shiny project, where the chart adjust automatically the axis scales and adds trend lines the the chart among other things (I did not include for simplicity) that I do not know how to do it in plotly.
Many thanks in advance. I have tried a million ways for a couple of days now, and did not succeed.
# choose mtcars data and add rowname as column as I want to link it to shapes in ggplot
data1 <- mtcars
data1$model <- rownames(mtcars)
# I turn cyl data to character as when charting it showed (Error: Continuous value supplied to discrete scale)
data1$cyl <- as.character(data1$cyl)
# linking colors with cylinders and shapes with models
ccolor <- c("#E57373","purple","green")
cylin <- c(6,4,8)
# I actually do not want shapes to be different, only want to show data of model when I point the data point.
models <- data1$model
sshapes <- rep(16,length(models))
# I am going to chart, do not want legend to show shape
graff <- ggplot(data1,aes(x=disp, y=wt,shape=model,col=cyl)) +
geom_point(size = 1) +
ylab ("eje y") + xlab('eje x') +
scale_color_manual(values= ccolor, breaks= cylin)+
scale_shape_manual(values = sshapes, breaks = models)+
guides(shape='none') # do not want shapes to show in legend
graff
chart is fine, but when converting to ggplotly, I am having trouble with the legend
# chart is fine, but when converting to ggplotly, I am having trouble with the legend
graffPP <- ggplotly(graff)
graffPP
legend is not the same as it was in ggplot
I succeeded in showing the model and data from axis when I point a datapoint in the chart... but now I am having problems with the legend....
To the best of my knowledge there is no easy out-of-the box solution to achieve your desired result.
Using pure plotly you could achieve your result by assigning legendgroups which TBMK is not available using ggplotly. However, you could assign the legend groups manually by manipulating the plotly object returned by ggplotly.
Adapting my answer on this post to your case you could achieve your desired result like so:
library(plotly)
p <- ggplot(data1, aes(x = disp, y = wt, shape = model, col = cyl)) +
geom_point(size = 1) +
ylab("eje y") +
xlab("eje x") +
scale_color_manual(values = ccolor, breaks = cylin) +
scale_shape_manual(values = sshapes, breaks = models) +
guides(shape = "none")
gp <- ggplotly(p = p)
# Get the names of the legend entries
df <- data.frame(id = seq_along(gp$x$data), legend_entries = unlist(lapply(gp$x$data, `[[`, "name")))
# Extract the group identifier, i.e. the number of cylinders from the legend entries
df$legend_group <- gsub("^\\((\\d+).*?\\)", "\\1", df$legend_entries)
# Add an indicator for the first entry per group
df$is_first <- !duplicated(df$legend_group)
for (i in df$id) {
# Is the layer the first entry of the group?
is_first <- df$is_first[[i]]
# Assign the group identifier to the name and legendgroup arguments
gp$x$data[[i]]$name <- df$legend_group[[i]]
gp$x$data[[i]]$legendgroup <- gp$x$data[[i]]$name
# Show the legend only for the first layer of the group
if (!is_first) gp$x$data[[i]]$showlegend <- FALSE
}
gp

line in ggplot connects all dots and not only those in group

I have molten dataframe and wants to plot all the different variables with a line. However when i do that GGplot connects all lines and the plot is meaningless.
df <-data.frame (names =c("a","b","c","a","b","c"),
time = c(1,1,1,2,2,2),
xvar = c(150,37,38,150,50,50))
ggplot(df,aes(x=time, y=xvar), group = names)+
geom_line()
Can this be fixed, so the plot shows three lines connecting the dots in each variable
You put the group outside of the aes(). It needs to be inside the aes():
ggplot(df,aes(x=time, y=xvar, group = names)) +
geom_line()

Plotting a line graph

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

Using two data frames in ggplot and having trouble with the legend

I have plotted x and y variables in a scatter plot with a legend that provides a shape and color for each point based off a single sample id variable. I want to overlay points from a second data frame, however when I try to add in the points from the second data frame I get an error saying it can't find the variable that I used to specify the color and shape of the points from the original data frame. I am using this code:
p=ggplot(HebWater, aes(x = PCSrCa.24, y = SrIso, group = Location,
color = Location, shape = Location)) +
geom_point(size=6) +
scale_shape_manual(values = 1:17) +
theme(panel.grid.major=element_line(colour="white"),
panel.grid.minor=element_blank(),panel.background=element_rect(colour="black",fill="white"))
p
p1=p+geom_errorbar(aes(ymin=SrIso-SDSrIso*3, ymax=SrIso+SDSrIso*3))+
geom_errorbarh(aes(xmin=PCSrCa.24-SDSrCa*3, xmax=PCSrCa.24+SDSrCa*3))
p1
p2=p1+geom_point(data=HebOto, aes(SrCa,SrIso))
p2
Everything works fine until I try to run the code for graph p2, I was already successful plotting both data frames on the same graph but was not able to get the legend to display properly no matter how I tried to change the shape, and color parameters using this code:
ggplot(HebWater, aes(PCSrCa.24, SrIso))+
geom_errorbar(aes(ymin=SrIso-SDSrIso*3, ymax=SrIso+SDSrIso*3))+
scale_shape_manual(values=1:17)+
geom_errorbarh(aes(xmin=PCSrCa.24-SDSrCa*3, xmax=PCSrCa.24+SDSrCa*3))+
geom_point(data=HebOto, aes(SrCa,SrIso))+
geom_point(data=HebWater, show_guide=TRUE, shape=c(1:17), colour=c(1:17), size=6)+
theme(panel.grid.major=element_line(colour="white"),
panel.grid.minor=element_blank(),
panel.background=element_rect(colour="black",fill="white"))
My data frames are both organized in this fashion:
> head(HebWater)
Location SrIso PCSrCa.24 SDSrIso SDSrCa PCSrCa.28
1 Gib (baseflow) 0.70966 0.2911440 0.000643719 0.0308056 0.3396680
2 Fire (baseflow) 0.71006 0.1119312 0.000643719 0.0308056 0.1305864
3 Mad R (runoff) 0.71052 0.2043264 0.000643719 0.0308056 0.2383808
HebWater <- data.table(
SrIso = runif(20),
SDSrIso = runif(20),
PCSrCa.24 = runif(20),
SDSrCa = runif(20)
)
HebOto <- data.table(
SrCa = runif(20),
SrIso = runif(20)
)
library(ggplot2)
ggplot(HebWater, aes(PCSrCa.24, SrIso))+
geom_errorbar(aes(ymin=SrIso-SDSrIso*3, ymax=SrIso+SDSrIso*3))+
scale_shape_manual(values=1:17)+
geom_errorbarh(aes(xmin=PCSrCa.24-SDSrCa*3, xmax=PCSrCa.24+SDSrCa*3))+
geom_point(data=HebOto, aes(SrCa,SrIso))+
geom_point(data=HebWater, show_guide=TRUE, shape=c(1:17), colour=c(1:17), size=6)
Produces this error:
Error: Incompatible lengths for set aesthetics: shape, colour, size
This seems logical to me since you fix the shape, color and size of your points in the second geom_point(). There is no legend to give since those do not depend at all on the data! If you want a legend, you have to use aes(shape=some_variable1, colour=some_variable2, size=some_variable3) and then if necessary force the randering with scale_xxxxxx_manual().

Plotting error while using ggplot faceting function in R

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)

Resources