ggplot2: Some geom_point shape result in strange color - r

I am facing a rather weird problem. I want the geom_pointto have the shape |. Doing so gives me a plot with some discolouration. There seems to be some green and yellow color although I set colors to black. I thought I was wrong but saving the plot and zooming in confirmed my feeling.
I am doing this
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
geom_point(shape= 124, size= 20, col= "black")
What I get is this (I zoomed in to clarify the problem):
You may have to click on the image to see what I mean. Interestingly, this does not happen if I use shape= 1:

I think this is simply imaging artefact rather than anything to do with ggplot as such. If you save the plot in vector format like pdf and zoom way in, the chromatic fringes aren't "really" there:

In addition to user Allan Camerons answer, I don't think this is an image artefact (nor chromatic aberration :). This seems to be intended and part of anti-aliasing, called subpixel rendering.
Related: See also this thread .
Possibly, it may be a sort of convention to display black with red pixels shown left, and blue pixels shown to the right (??), and therefore subpixel rendering would show those colors to each side.

Related

Can I place my ggplot2 legend right beside the horizontal axis label?

In order to save as much as white space as I can, I would like to place the legend entry at the same height as the horizontal axis label. Can this be done and if so, how?
Here's a plot illustrating what I am hoping to achieve, with current and hoped-for legend position illustrated using a (manually added) green box.
I currently place the legend using theme(legend.position=c(0.87,0.1)) (noting that the exact coordinates are not relevant). Ideally, this route would allow for values outside of the [0,1] domain but it appears not to allow for that.
theme(legend.position="bottom") places the legend well outside of the plotting area, thus taking up more white space than I am willing to spare.
You just might have to play around with negative values regarding the y-coordinates of your legend.position-vector.
Here's an example:
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length, color=Species))+
geom_line()+
facet_wrap(~Species)+
theme(legend.position=c(0.87,-0.01))
Note the -0.01 value. Is this what you're looking for?

PyQtGraph GLScatterPlotItem: How to make colors opaque

In PyQtGraph, GLScatterPlotItem, I would like the points to not blend color together when the points overlap. I want to see the closest point, and not the ones behind.
I have asked for the colors to be opaque (alpha = 1.0), but when the dots in a plot overlap, the color just turns a shade of magenta, even if all the points in that region arevery similar color.
Here's an example:
plt = gl.GLScatterPlotItem(pos=coords, color = colors, size=5, pxMode=True)
where colors are a sort of 'heat map' that range from red to blue.
The plot I get is this:
You can see there is some red, but everywhere the points really overlap, the color goes weird.
On the other hand, if I do the simple modification of size=1, then the colors are nice, but the dots are tiny, and can be hard to see:
This is exactly the same data both time. You can start to see a little of the magenta color over to the left and rear where point desity is high, but other than that, the colors are correct.
How can I prevent the magentification of my plots?
Thanks a bunch!
OpenGL is probably rendering dots additively and saturating. Try:
plt.setGLOptions('opaque')
I ran into this as well; the default options are pretty for volumetric data, but aren't great for dense point clouds from surfaces. If you figure out something even better, i.e. local patches with illumination, post back about it!

ggplot legend list is larger than page

I have a plot in R which has a very large number of sample groups, and therefore the legend is larger than the page size and is cut off. I understand that this is not publication quality, but I need to know the colours to be able to make the legend in Illustrator.
Is there a way to make the page size much bigger or somehow change the legend format so that I can include all the keys? The reason for this is so that I can open the PDF in Illustrator and get the colours for each sample to create a new legend that will be for publication. I thought that maybe there is a clipping mask, and that the actual legend will be preserved, but when I opened in Illustrator, the legend was actually cut at the page ends1.
As was suggested in the comments below I gave nrow a try which helped break the legends up but now the entire page is just legends.
ggplot(purine.n, aes(x=variable, y=value, colour=metabolite_gene, shape=variable))
+geom_abline(slope=0)
+geom_point(size=4, position=position_dodge(width=0.08))
+scale_y_continuous(limit=c(-3.5,5.5), breaks=c(-3,-2,-1,0,1,2,3,4,5))
+scale_shape_manual(values=c(16,17,17), guide=F)
+theme_bw()
+theme(legend.key=element_blank(), legend.key.size=unit(1,"point"))
+guides(colour=guide_legend(nrow=16))
As was suggested in the comments, nrow was the answer to my problem. I had to adjust the value to get the right number of rows to fit my legend. Below is the completed code that worked. There's more tweaking I need to do, like change page size to help make things look better, but that is out of the scope of this question.
ggplot(data.n, aes(x=variable, y=value, colour=metabolite_gene, shape=variable))
+geom_abline(slope=0)+geom_point(size=4, position=position_dodge(width=0.08))
+scale_y_continuous(limit=c(-3.5,5.5), breaks=c(-3,-2,-1,0,1,2,3,4,5))
+scale_shape_manual(values=c(16,17,17), guide=F)
+theme_bw()
+theme(legend.key=element_blank(), legend.key.size=unit(1,"point"))
+guides(colour=guide_legend(nrow=30))

geom_bar not showing every values

I want to draw a bar plot, with ggplot and geom_bar, but it seems that the behavior of geom_bar is not consistent. I don't understand why.
My data is a time series of precipitations:
library(ggplot2)
library(data.table)
library(lubridate)
set.seed(42)
dt1 <- data.table(dateHeure=seq(ymd_hms("2014-06-04 13:30:00"),
ymd_hms("2014-10-20 08:30:00"), by='1 hour'),
rain=sample(c(rep(5,15), rep(10,15), rep(20,10),
rep(30, 5), 40, rep(0, 3262))))
Then i plot it, and not all the data appears... Why is some data missing?
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain),
stat="identity",
fill="blue") # doesn't work!
But if i add the variable color in aes, then the plot is correct!
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain, color="rain"),
stat="identity",
width=0.2) # work properly
So someone know why geom_bar doesn't work properly without color? Because i can't rely on it if sometimes not all the data is correctly plotted...
thanks!
edit: to respond to #eipi10, i added the plots. The strange thing is that when i resize the plot window in the first case, the data which is plotted changes!
Based on the edit to your question, I think I know what's happening: In the first plot, you use fill="blue". But the bin widths are very small compared to the overall range of the x-axis. This results in very, very thin vertical bars--so thin that you can't see some of them on your screen, but they appear when you expand the physical width of the plot.
On the other hand, in your second plot you used colour="rain", which adds a border to each bar, making each bar thicker, so they are visible, even when the physical width of the plot is relatively small.
Try adding colour="blue"(or "red" or whatever) to your first plot and I think you'll see all the bars, even without resizing. On the other hand, try changing colour="rain" to fill="rain" on your second plot and see if that creates the "disappearing data" effect on your second plot.
UPDATE: In response to your comment, you can use the colour parameter and then set the line width to get exactly the bar thickness you want, so you don't really need fill. For example:
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain),
stat="identity",
colour="blue", lwd=0.5)
Just set lwd (line width) to a value that gives you the bar-width you want. And, of course, you can also change the colour as well.

how to change the gradient of colour in bplot for R

I was wondering if anybody had much experience with the function bplot in R, I am making a 3d plot and the plot works fine. The only thing I want to change is the gradient of colour which you get from drape=TRUE. At the moment it has a single pink colour fading into blue, I really need a third colour in the middle to highlight the central data better as this is the most important for my study, and at the moment in some of the plots I am doing its too difficult to pick out and correlate with the level of y in the colour scale bar.
Does anybody have any idea how to do this?
I need more reputation to post an image of the plot but you can see what I mean in the second image of this thread.
Plot Regression Surface
Many thanks
Aaron
Try adding a colorRampPalette argument to your plot like so:
col.regions = colorRampPalette(colors=c("red","yellow"))(1000)
This will give you a gradient of 1000 shades between red and yellow, You can use any of the R colors in the color ramp, and you can specify more than two e.g.colors=c("red","orange3","palegoldenrod") if you like. You should put this argument at the same place you are putting drape=TRUE

Resources