Unable to plot multiple line plots in the same graph - r

I have the following code and I want to draw two lines, both specified in the same data frame. However, I'm getting big coloured shadows, and I'm not able to figure out the cause. The data and the code look correct to me...
library('ggplot2')
library('reshape2')
df <- read.csv(url("http://smallchess.com/test.csv"), row.names=1)
melted = melt(df, id.vars='time')
p <- ggplot(data=melted, aes(x=time, y=value, group=variable, colour=variable)) + geom_line()
print(p)

The two variables show extremely oscillating values. So that each line overlaps its neighbor. Thus, this opaque structure is generated. Perhaps it helps if you set your size of the line to a low value like this:
p <- ggplot(data=melted, aes(x=time, y=value, group=variable, colour=variable)) +
geom_line(size = 0.05)
print(p)

Related

I am drawing a plot in ggplot in R and all I am getting as the result is a line with points in vertical direction with no x-labels or markings

ggplot(data=df, aes(x='Matcing_Probability', y=Locus_Name, group=1)) +
+ geom_line(color="#aa0022", size=1.75) +
+ geom_point(color="#aa0022", size=3.5)
This is the graph I am getting from the code.
You need to send ggplot2 symbols (unquoted column names) in aes() if you are assigning an aesthetic to a column in your dataset. Otherwise, it will assume you are sending the string of a new symbol. So:
# your original
ggplot(data=df, aes(x='Matching_Probability', y=Locus_Name, group=1))
# change to this:
ggplot(data=df, aes(x=Matching_Probability, y=Locus_Name, group=1))
Consider the difference in the following example to highlight why even more:
# this works fine
df <- data.frame(x=1:10, y=1:10)
ggplot(df, aes(x=x,y=y)) + geom_point()
# this doesn't:
ggplot(df, aes(x="x",y=y)) + geom_point()

ggplotly with geom_ribbon grouping

I'm having some problems in converting a ggplot in to a plotly object, and retaining the same legend attributes. What I want:
For grouped series, a single line for fit, and faded region for ribbon of same colour, with transparency
No lines at the edge of the ribbon
Grouped legends for the lines, points and ribbons
Here is the code showing the 2 approaches I tried based on this answer:
ggplot: remove lines at ribbon edges
Both have an undesirable effect as you can see when running. Any suggestions would be great :)
library(plotly)
library(ggplot2)
# fake data
set.seed(1)
dt <- data.frame(x=rep(1:7,2), group=rep(letters[1:2], each=7), value=runif(14))
dt$lwr <- dt$value*.9
dt$upr <- dt$value*1.1
# build plot in ggplot, don't want lines at the edge
pl <- ggplot(data=dt, aes(y=value, x=x, group=group, colour=group,
fill=group)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin=lwr, ymax=upr), alpha=.3, linetype=0) +
theme_minimal()
# looks ok, no lines at the edges
pl
# lines at edges, single group
ggplotly(pl)
# alternative: try reverting colour to NA
pl2 <- ggplot(data=dt, aes(y=value, x=x, group=group, colour=group,
fill=group)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin=lwr, ymax=upr), alpha=.3, colour=NA) +
theme_minimal()
# looks ok
pl2
# no lines, but now not grouped, and some weird naming
ggplotly(pl2)
Thanks, Jonny
EDIT:
Addition to the accepted answer, in functional form
# dd: ggplotly object
library(stringi)
library(rvest)
remove_ggplotly_ribbon_lines <- function(dd){
find <- rvest::pluck(dd$x$data, "fillcolor")
w <- which(!sapply(find, is.null))
for(i in w){
dd$x$data[[i]]$line$color <-
stringi::stri_replace_all_regex(dd$x$data[[i]]$line$color, ",[\\d.]*\\)$", ",0.0)")
}
return(dd)
}
remove_ggplotly_ribbon_lines(ggplotly(pl))
Hi this is more a comment than an answer but I do not have right to post comments.
If you investigate the ggplotly object you will see that it is actually just a list. Changing the right elements of the list helps in controlling plot options.
The solution below just changes the alpha of the lines at ribbon edges. Hope this helps
library(plotly)
set.seed(1)
dt <- data.frame(x=rep(1:7,2), group=rep(letters[1:2], each=7), value=runif(14))
dt$lwr <- dt$value*.9
dt$upr <- dt$value*1.1
# build plot in ggplot, don't want lines at the edge
pl <- ggplot(data=dt, aes(y=value, x=x, group=group, colour=group,
fill=group)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin=lwr, ymax=upr), alpha=.3, linetype=0) +
theme_minimal()
# looks ok, no lines at the edges
pl
# no lines at edges
dd = ggplotly(pl)
dd$x$data[[3]]$line$color = "rgba(248,118,109,0.0)"
dd$x$data[[4]]$line$color = "rgba(0,191,196,0.0)"
dd

Error when ordering grouped bars in ggplot2

My data is in the long format (as required to do the grouped barplot), so that the values for different categories are in one single column. The data is here.
Now, a standard barplot with ggplot2 orders the bars alphabetically (in my case of country names, from Argentina to Uganda). I want to keep the order of countries as it is in the dataframe. Using the suggestion here (i.e. ussing the limits= option inside the scale_x_discrete function) I get the following graph:
My code is this:
mydata <- read_excel("WDR2016Fig215.xls", col_names = TRUE)
y <- mydata$value
x <- mydata$country
z <- mydata$Skill
ggplot(data=mydata, aes(x=x, y=y, fill=z)) +
geom_bar(stat="identity", position=position_dodge(), colour="black") +
scale_x_discrete(limits=x)
The graph is nicely sorted as I want but the x axis is for some reason expanded. Any idea what is the problem?
this?
mydata$country <- factor(mydata$country, levels=unique(mydata$country)[1:30])
ggplot(data=mydata, aes(x=country, y=value, fill=Skill)) +
geom_bar(stat="identity", position=position_dodge(), colour="black")

Arranging data for two facet R line plot

I am trying to make a two facet line plot as this example. My problem is to arrange data to show desired variable on x-axis. Here is small data set I wanna use.
Study,Cat,Dim1,Dim2,Dim3,Dim4
Study1,PK,-3.00,0.99,-0.86,0.46
Study1,US,-4.67,0.76,1.01,0.45
Study2,FL,-2.856,4.15,1.554,0.765
Study2,FL,-8.668,5.907,3.795,4.754
I tried to use the following code to draw line graph from this data frame.
plot1 <- ggplot(data = dims, aes(x = Cat, y = Dim1, group = Study)) +
geom_line() +
geom_point() +
facet_wrap(~Study)
As is clear, I can only use one value column to draw lines. I want to put Dim1, Dim2, Dim3, Dim4 on x axis which I cannot do in this arrangement of data. [tried c(Dim1, Dim2, Dim3, Dim4) with no luck]
Probably the solution is to transpose the table but then I cannot reproduce categorization for facet (Study in above table) and colour (Cat in above table. Any ideas how to solve this issue?
You can try this:
library(tidyr)
library(dplyr)
gather(dims, variable, value, -Study, -Cat) %>%
ggplot(aes(x=variable, y=value, group=Cat, col=Cat)) +
geom_point() + geom_line() + facet_wrap(~Study)
The solution was quite easy. Just had to think a bit and the re-arranged data looks like this.
Study,Cat,Dim,Value
Study1,PK,Dim1,-3
Study1,PK,Dim2,0.99
Study1,PK,Dim3,-0.86
Study1,PK,Dim4,0.46
Study1,US,Dim1,-4.67
Study1,US,Dim2,0.76
Study1,US,Dim3,1.01
Study1,US,Dim4,0.45
Study2,FL,Dim1,-2.856
Study2,FL,Dim2,4.15
Study2,FL,Dim3,1.554
Study2,FL,Dim4,0.765
Study2,FL,Dim1,-8.668
Study2,FL,Dim2,5.907
Study2,FL,Dim3,3.795
Study2,FL,Dim4,4.754
After that R produced desire result with this code.
plot1 <- ggplot(data=dims, aes(x=Dim, y=Value, colour=Cat, group=Cat)) + geom_line()+ geom_point() + facet_wrap(~Study)

plotting the whole data within each facet using facet_wrap and ggplot2

I am trying to plot line graphs for and facet_wrap for each dataset. What I would love to have is in light grey, transparent or something, all datasets in the background.
df <- data.frame(id=rep(letters[1:5], each=10),
x=seq(10),
y=runif(50))
ggplot(df, aes(x,y, group=id)) +
geom_line() +
facet_wrap(~ id)
This graph is how far I get, but I would love to have all the other missing 4 lines in each graph as well... In any way I try to use facet_wrap, I get only the data of a single line.
What I would expect is something like this for each facet.
ggplot(df, aes(x,y, group=id)) +
geom_line() +
geom_line(data=df[1:10,], aes(x,y, group=id), size=5)
Here's another approach:
First add a new column identical to id:
df$id2 <- df$id
Then add another geom_line based on the df without the original id column:
ggplot(df, aes(x,y, group=id)) +
geom_line(data=df[,2:4], aes(x=x, y=y, group=id2), colour="grey") +
geom_line() +
facet_wrap(~ id)
Here is an approach. It might not be suitable for larger datasets, as we replicate the data number_of_facets-times.
First, we do some data-wrangling to create this desired dataframe.
df$obs_id <- 1:nrow(df) #unique ID for each observation
#new data with unique ID's and 'true' facets
df2 <- expand.grid(true_facet=unique(df$id), obs_id=1:nrow(df))
#merge them
dat <- merge(df,df2,by="obs_id",all=T)
Then, we create a flag defining the 'true' faceted variable, and to discern background from foreground.
dat$col_flag <- dat$true_facet == dat$id
Now, plotting is easy. I've used geom_line twice instead of scales, as that was easier than to try to fix the ordering (would lead to black being plotted below grey).
p1 <- ggplot(dat, aes(x=x,y=y, group=id))+
geom_line(color="grey")+
geom_line(dat=dat[dat$col_flag,],size=2,color="black")+
facet_wrap(~true_facet)

Resources