How do I make a line invisible in ggplot2 in R? - r

I wish to do this instead of doing subsets so that my confidence intervals reflect all of the data. (I'm plotting lines one by one on a powerpoint.)
Basically, I need to just have one line be completely invisible.
I've tried
scale_color_manual(values=c("black", "transparent"))
which seems to do the trick, unless the transparent line overlaps another, in which case it makes it lighter. "none" and "invisible" are not defined color values in R. Advice?
Thanks!

You can specify NA as a color if you just don't want something plotted:
values = c("black", NA)

Related

Different color points in R

I have the following R-code,
x1=c(3,2,4,1,2,4,4)
x2=c(4,2,4,4,1,3,1)
Y=c("red","red","red","red","blue","blue","blue")
plot(x1,x2,col=Y,pch=8)
grid(NULL,NULL,col="cornsilk2")
legend("right",c("Point","star"),col=c("red","blue"))
That creates a plot as seen below
There are two things that I wish to change however I am not sure how to go about it.
1) I want to change the types of points that appear using the pch feature in plot. So for example, I want the red points to appear as a star and the blue points to appear as a triangle. How would I go about this?
2) I want the legend to show those symbols and be coloured respectively correctly. For example, instead of having "Point" it should be a "." that is coloured blue or red depending on what colour I decide to assign it.
Many thanks for the help.
You specify a vector like your color:
SHAPE = ifelse(Y=="red",8,2)
plot(x1,x2,col=Y,pch=SHAPE)
legend("right",c("Point","star"),col=c("blue","red"),pch=c(2,8))

Mixed geom_line & geom_point plot: remove marker from color scale

I often have to use plots mixing lines and points (ggplot2), with the colors of the line representing one variable (here, "Dose"), and the shape of the points another one (here, "Treatment). Figure 1 shows what I typically get:
Figure 1: what I get
I like having different legends for the two variables, but would like to remove the round markers from the color scale, to only show the colors (see legend mockup below, made with Gimp). Doing so would allow me to have a clean legend, with colors and shapes clearly segregated.
Figure 2 (mockup): what I would like
Would anyone know if there is a way to do that? Any help would be much appreciated.
Note: the plots above show means and error bars, but I have the same problem with any plot mixing geom_line and geom_point, even simple ones.
Thanks in advance !

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

How do I use ggplot2 to manually assign hexadecimal colors to each data point in a plot or bar graph?

I have a data set, and one of the variables is a factored array with hexadecimal characters (e.g. '#00FF00'). One of the things I wanted to try doing is creating a bar plot with all of the different colors combined.
I tried using
cg<-ggplot(my.data,aes(x=factor(1),fill=as.character(my.color)))
followed by
cg+geom_bar()
but the only colors plotted seem to be ones from the default scale. I've tried omitting the as.character() part of the code, but it doesn't make a difference. I also have the same issue when making 2d plots with geom_point().
If I try something like
plot(my.data$var1,my.data$var2,col=as.character(my.color))
the colors are plotted the way I wanted them, although the graph doesn't look as nice as the ones in ggplot2.
Is there something obvious I'm missing, or is this beyond the scope of ggplot2?
You should add scale_fill_identity() to use color names as actual colors.
ggplot(my.data,aes(x=factor(1),fill=my.color)) +
geom_bar()+
scale_fill_identity()

Resources