Omitting some legends in ggplot2 - r

Is there a way to partially suppress legend in ggplot2 ? For example for chart below
There are way too many colours in the legend. Let's say I still want to display all colours, but only want to show the legend for letters b to e. Is there any way to do so?

Keeping in mind the comments above, you may use the following solution. The linked answer proposes changing limits, but another possibility is to override breaks. Here's how:
let <- letters[1:20]
let_be <- let
p <- qplot(1:20, 1:20, colour = let)
let_be[!(let %in% c("b", "e"))] <- NA
p + scale_color_discrete(breaks = let_be)

Related

how to change the color scale for each graph with facet_wrap and legend

I have a question about facet_wrap() in ggplot2.
I am trying to make a graph that looks like this. I attach an example image 1.enter image description here
In image 1 it can be seen that there are two maps and each one has its legend and color scale. I would like to be able to do this with ggplot and the facet_wrap() function.
My problem is that because the data in the dataframe is very different, they have a lot of amplitude for each map, when plotting the scale it does not allow me to visualize it the way I want.
enter image description here
ggplot(dataframe,mapping=aes(x=lon,x=lat))+
geom_contour_fill((aes(z=hgt,fill=stat(level)))+
geom_contour(aes(z=hgt),color="black",size=0.2)+
scale_fill_distiller(palette = "YlOrBr",direction = 1,super=ScaleDiscretised)+
mi_mapa+
coord_quickmap(xlim = range(dataframe$lon),ylim=range(dataframe$lat),expand = FALSE)+
facet_wrap(~nombre_nivel,scales="free", ncol =2) +
labs(x="Longitud",y="Latitud",fill="altura",title = "campos")
my dataframe has a shape like this. Where the facets are determined by the level variable. In this case the dataframe has another variable which is temp instead of hgt, but it's just another name.
enter image description here
Thanks
I think I've faced the alike problem building the two parts of the single map with two different scales. I found the package grid useful.
library(grid)
grid.newpage()
print(myggplot, vp = specifiedviewport)
In my case I built the first p <- ggplot() than adjusted p2 <- p + ...
with printing the two ggplots (p and p2) in two viewports. You can newly construct p2 with individual scale and print it in the grid. You can find useful information
here.

Adding to a legend after each iteration in R

I am plotting several regression lines, distinguished by setting lty, into one graph in R by using the abline command.
Now, I am now trying to add a legend to the plot to help readers interpret it. It's supposed to be a very basic legend:
Linetype X = Variable 1
Linetype Y = Variable 2, etc.
Now the documentation on legend() makes total sense to me, but I have only succeeded in writing out one legend at a time.
Is there any way I can iteratively build this legend? I.e. add
linetype and variable to a blank legend while plotting?
Or do I have to collect all the info and write it after the plotting
is done? If so, what would be the most elegant way to do this?
It would be great if I could rely on standard packages for this to make the code more portable.
Apparently there are ways for doing this in Matlab, but I could not find anything for R.
Here is my code:
cols=c(0:length(v))
count=1
for (v in variables)
{
...
lmodel=lm(v~x);
abline(lmodel, lty=cols[count]);
count=count+1
...
}
}
Any help is much appreciated!
This minimal working example of collecting the legend text from all repetitions of your loop should be easy to adapt for your problem:
# dummy plot
plot(iris[,1:2])
# empty legend text
legend_text <- c()
for (v in 1:4) {
abline(v,0, col = v, lty = v)
# add next legend text
legend_text <- c(legend_text, v)
}
# plot legend once
legend('topright', legend = legend_text, lty=1:4, col=1:4)
One more thing: if you run into problems with using a vector, consider using list() instead.

Set categorical axis labels with scales "free" ggplot2

I am trying to set the labels on a categorical axis within a faceted plot using the ggplot2 package (1.0.1) in R (3.1.1) with scales="free". If I plot without manually setting the axis tick labels they appear correctly (first plot), but when I try to set the labels (second plot) only the first n labels are used on both facets (not in sequence as with the original labels).
Here is a reproducible code snippet exemplifying the problem:
foo <- data.frame(yVal=factor(letters[1:8]), xVal=factor(rep(1:4,2)), fillVal=rnorm(8), facetVar=rep(1:2,each=4))
## axis labels are correct
p <- ggplot(foo) + geom_tile(aes(x=xVal, y=yVal, fill=fillVal)) + facet_grid(facetVar ~ ., scales='free')
print(p)
## axis labels are not set correctly
p <- p + scale_y_discrete(labels=c('a','a','b','b','c','d','d','d'))
print(p)
I note that I cannot set the labels correctly within the data.frame as they are not unique. Also I am aware that I can accomplish this with arrange.grid, but this requires "manually" aligning the plots if there are different length labels etc. Additionally, I would like to have the facet labels included in the plot which is not an available option with the arrange.grid solution. Also I haven't tried viewports yet. Maybe that is the solution, but I was hoping for more of the faceted look to this plot and that seems to be more similar to grid.arrange.
It seems to me as though this is a bug, but I am open to an explanation as to how this might be a "feature". I also hope that there might be a simple solution to this problem that I have not thought of yet!
The easiest method would be to create another column in your data set with the right conversion. This would also be easier to audit and manipulate. If you insist on changing manually:
You cannot simply set the labels directly, as it recycles (I think) the label vector for each facet. Instead, you need to set up a conversion using corresponding breaks and labels:
p <- p + scale_y_discrete(labels = c('1','2','3','4','5','6','7','8'), breaks=c('a','b','c','d','e','f','g','h'))
print(p)
Any y axis value of a will now be replaced with 1, b with 2 and so on. You can play around with the label values to see what I mean. Just make sure that every factor value you have is also represented in the breaks argument.
I think I may actually have a solution to this. My problem was that my labels were not correct because as someone above has said - it seems like the label vector is recycled through. This line of code gave me incorrect labels.
ggplot(dat, aes(x, y))+geom_col()+facet_grid(d ~ t, switch = "y", scales = "free_x")+ylab(NULL)+ylim(0,10)+geom_text(aes(label = x))
However when the geom_text was moved prior to the facet_grid, the below code gave me correct labels.
ggplot(dat, aes(x, y))+geom_col()+geom_text(aes(label = x))+facet_grid(d ~ t, switch = "y", scales = "free_x")+ylab(NULL)+ylim(0,10)
There's a good chance I may have misunderstood the problem above, but I certainly solved my problem so hopefully this is helpful to someone!

Use R to make a barplot with bar colors determined by the height of the bar?

I would like to use R to make a barplot of ~100,000 numerical entries. The plot will be dense, which is what I want. So far I am using the following code:
sample_var <- c(2,5,3,2,3,2,6,10,20,...) #Filled with 100,000 entries
barplot(sample_var)
The resulting plot is just what I want, but I would like to make a conditional formatting statement so that bars less than 5 will be black, bars >= 5 and <= 10 are green, and bars > 10 are red.
Any help is appreciated!
Update: Looking at other solutions, "easily" was an overstatement. However, I'll leave my answer here for reference. Look at my other answer for a solution which does not require ggplot2.
You can use the ggplot2 package to produce that plot easily, using the bar geometry and identity statistic.
library(ggplot2)
sample_var <- log(runif(10000) + 1)
ggplot(data.frame(x=seq(1:length(sample_var)), y=sample_var), aes(x=x, y=y, fill=y)) + geom_bar(stat="identity")
If you want a simple answer, how about using the next vector as colors.
colors = as.character(cut(sample_var,breaks=c(0,5,10,20),labels=c('black','green','red')))
I do not quite remember where the inequalities are set in cut() but a simple help should clear everything.
But more importantly, do not make a barplot of 100000 entries.
You can use ?ifelse to create a vector of colors and include that in the call to barplot. To make it possible for the colors to show up with so many bars, do not include a border around your bars (h/t to #musically_ut).
set.seed(1) # this will allow you to get exactly the same data
# this generates data to use for the example plot:
sample_var <- rpois(100000, lambda=5)
cols <- ifelse(sample_var<=5, "black",
ifelse(sample_var<=10, "green", "red"))
barplot(sample_var, col=cols, border=NA)
I find nested ifelse()'s ugly and so generally use findInterval to do selections from disjoint choices over a range of intervals. This is an alternative to #gung's answer:
set.seed(1)
sample_var <- rpois(100000, lambda=5)
cols <- c("black", "green", "red") [findInterval(samplevar, c(-Inf, 5, 10, Inf) ) ]
barplot(sample_var, col=cols, border=NA)
This has the advantage that it's very easy to change the cutpoints and colors. (no need to put in an image; it's identical to gung's image.
Adding a separate answer which does not use ggplot2 but the native R functions.
You can use the palette functions in R to generate a gradient to suit your granularity:
sample_var <- log(runif(100000) + 1)
max.colors <- 1000
cols <- heat.colors(max.colors)
barplot(sample_var, col=cols[ max.colors - floor(max.colors * sample_var / max(sample_var)) ], border=NA)
There are some artefacts of condensing 100,000 lines into 800 or so pixels visible here. Some of the bars (periodically) are absent.

How to plot matrix with background color varying according to entry?

I wanted to ask for any general idea about plotting this kind of plot in R which can compare for example the overlaps of different methods listed on the horizontal and vertical side of the plot? Any sample code or something
Many thanks
A ggplot2-example:
# data generation
df <- matrix(runif(25), nrow = 5)
# bring data to long format
require(reshape2)
dfm <- melt(df)
# plot
require(ggplot2)
ggplot(dfm, aes(x = Var1, y = Var2)) +
geom_tile(aes(fill = value)) +
geom_text(aes(label = round(value, 2)))
The corrplot package and corrplot function in that package will create plots similar to what you show above, that may do what you want or give you a starting point.
If you want more control then you could plot the colors using the image function, then use the text function to add the numbers. You can either create the margins large enough to place the text in the margins, see the axis function for the common way to add text labels in the margin. Or you could leave enough space internally (maybe use rasterImage instead of image) and use text to do the labelling. Look at the xpd argument to par if you want to add the lines and the grconvertX and grconvertY functions to help with the coordinates of the line segents.

Resources