Adjusting the width of legend for continuous variable - r

I have a script below to illustrate my question:
temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
# so far so good, but I think the legend positioned at bottom with such a small size is a waste of space, so I want to "widen" it using the line below...
chart <- chart + guides(colour=guide_legend(keywidth=5,label.position="bottom"))
# oops, it changed to legend for categorical variable
How can I widen the "continuous variable" legend positioned at bottom?

Instead of function guides() you should use the function theme() and set the legend.key.width=
temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
chart <- chart + theme(legend.key.width=unit(3,"cm"))

You can use guide_colourbar instead of guide_legend in your code :
temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE)
chart <- ggplot(data=temp.df,aes(x=x,y=y))
chart <- chart + geom_line(aes(colour=z))
chart <- chart + scale_colour_continuous(low="blue",high="red")
chart <- chart + theme(legend.position="bottom")
chart + guides(colour=guide_colourbar(barwidth=30,label.position="bottom"))

Related

Scroll bar & Formatting in gantt chart using ggplot

I am trying to plot a gantt chart and have created it successfully, but being too many values involved, it is visually not presentable.
What should i add to the below code to add a vertical scroll bar in the chart. Also, how can i assign customized colors to the bar & change the background & font.
df <- read.csv("C:/Users/XXXX/Downloads/XXXX.csv" , stringsAsFactors = F)
df$Timeline.Start <- as.Date(df$Timeline.Start, format = "%m/%d/%Y")
df$Timeline.End <- as.Date(df$Timeline.End, format = "%m/%d/%Y")
df$System.Lease.ID <- as.character(df$System.Lease.ID)
ggplot(df, aes(colour= Timeline.Type, group = System.Lease.ID)) +
geom_segment(aes(x=Timeline.Start, xend=Timeline.End, y=System.Lease.ID, yend=System.Lease.ID), size=3) +
labs(x="Timeline.Start", y="System.Lease.ID", title="Project timeline")

ggolot horizontal gaps contour

I've been trying to plot contour plots using ggplot2 and csv file. I can't figure out why there are horizontal gaps showing up across the image.
Here is the code:
library(ggplot2)
plot2 <- ggplot(data=thirtyfour,aes( x = X.m., y = Z.m., z = t_ca.2))
plot2
plot2 + geom_tile(aes(fill = t_ca.2) )+
scale_fill_continuous(limits=c(0,0.0219),
breaks=seq(0,0.0219, by=0.01),
low="blue",
high="yellow")
In the geom_tile aesthetic try adjusting the height parameter.
+geom_tile(aes(fill=t_ca.2, height=1)) +...
Otherwise, please provide reproducible example code.

Plot line on top of stacked bar chart in ggplot2

I have created a stacked bar chart, and I would now like to plot a line on the same graphic, but I can't figure it out. I've added the geom_line() to the ggplot call, but I only end up with the line, not the bar chart.
library(ggplot2)
library(reshape)
# First let's make a toy dataset for our stacked plot/line plot example.
year = c(1,2,3,4,5,6)
stocks = c(2,4,3,2,4,3)
exports = stocks*2
domestic = stocks*3
production = c(15,16,15,16,15,16)
# Make 2 df's: alldata is for stacked bar chart, linedata is for plotting a line on top of it.
alldata = data.frame(year,stocks,exports,domestic)
linedata = data.frame(year,production)
# Make alldata 'long' for the stacking
melteddata = melt(alldata,id.vars="year")
# This works fine: (but hooboy was tricky to figure out the ordering w/ stat="identity" )
plotS1 <- ggplot(melteddata, aes(x=year,y=value,factor=variable,fill=variable,order=-as.numeric(variable)))
plotS1 + geom_bar(stat="identity")
# This plots only the line, not the stacked bar chart :
plotS1 <- ggplot(melteddata)
plotS1 + geom_bar(aes(x=year,y=value,factor=variable,fill=variable,order=-as.numeric(variable)), stat="identity")
plotS1 + geom_line(data=linedata, aes(x=year,y=production))
You were close:
plotS1 <- ggplot(melteddata)
plotS1 + geom_bar(aes(x=year,y=value,factor=variable,fill=variable,
order=-as.numeric(variable)), stat="identity") +
geom_line(data=linedata, aes(x=year,y=production))

plotly labels in R stacked area chart

I am trying to make a stacked area chart in R exactly like this ggplot2 one (below) only using plotly.
Here is a link to my data.
To generate a plotly version of the above ggplot2 chart, I first have to add the values of each column in my dataframe, elw, on top of the values in the previous column like so. This is because plotly (as far as I'm aware) does not have the ability to automatically stack values in area charts.
With this new stacked data set, elw_stack, I use the following code to make my plotly chart:
el_plot2 = ggplot() +
geom_area(aes(elw_stack$year, elw_stack$x99999, fill = 'green')) +
geom_area(aes(elw_stack$year, elw_stack$x20000, fill = 'red')) +
geom_area(aes(elw_stack$year, elw_stack$x19000, fill = 'blue')) +
geom_area(aes(elw_stack$year, elw_stack$x12018, fill = 'purple')) +
geom_area(aes(elw_stack$year, elw_stack$x10006, fill = 'yellow'))
ggplotly(el_plot2)
That code generates this chart:
The issue is that the plotly labels refer to the cumulative elw_stack values. The green value pictured at year 1999 is actually ~3700 (i.e. 11,365 - 7957). But the description bar says the cumulative value of 11,365. Is there a way to fix this so that the labels aren't cumulative values?
I was having a similar problem and eventually decided not to use ggplotly, but instead i used the plot_ly function. Here is the code I used with your data:
elw <- read.csv("elw.csv")
elw_stack <- read.csv("elw_stack.csv")
plot <- plot_ly(data=elw_stack, x=year, y=x10006, fill="tonexty", mode="lines",
text=round(elw$x10006, 0), hoverinfo='x+text+name', name="x10006")
plot <- add_trace(plot, data=elw_stack, x=year, y=x12018, fill="tonexty", mode="lines",
text=round(elw$x12018,0), hoverinfo='x+text+name', name="x12018")
plot <- add_trace(plot, data=elw_stack, x=year, y=x19000, fill="tonexty", mode="lines",
text=round(elw$x19000,0), hoverinfo='x+text+name', name="x19000")
plot <- add_trace(plot, data=elw_stack, x=year, y=x20000, fill="tonexty", mode="lines",
text=round(elw$x20000,0), hoverinfo='x+text+name', name="x20000")
plot <- add_trace(plot, data=elw_stack, x=year, y=x99999, fill="tonexty", mode="lines",
text=round(elw$x99999,0), hoverinfo='x+text+name', name="x99999")
plot <- layout(plot, yaxis=list(title="Whatever title you wanna use"))
And this is how the final plot looks:
plotly image
What I can't get to work is to add the different traces using a for loop. I wanted to write a function that takes a data frame with an arbitrary number of columns as input and returns the stacked area plot, but for some reason the plot won't show all the traces (only first and last)
Hope it helps...

Add ticks without labels on the top of a bar plot in ggplot2

As the title says, I would like to add ticks to the TOP part of the bar plot in ggplot2. The output would look something like this: (hiding the actual plot due to confidential information). Is there a function in ggplot2 that does this?
I have adapted Baptiste's solution at link Display y-axis for each subplot when faceting. Idea (i think) is to extract the x-axis grob and add it to the top of the plot.
library(ggplot2)
library(gtable)
# plot
p1 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar() + theme_bw()
gg <- ggplotGrob(p1)
axis <- gtable_filter(gg ,"axis-b")[["grobs"]][[1]][["children"]][["axis"]][1,]
panels <- subset(gg$layout, name == "panel")
gg <- gtable_add_grob(gg, grobs=axis, name="ticks", t = panels$t-1, l=panels$l)
grid.newpage()
grid.draw(gg)

Resources