Plot two conditions in one plot - r

i'm sorry to ask such a dumb question, but i can't find an answer ...
So i have this table called : "linearsep" :
color x y
1 red 1 1
2 red 1 3
3 red 3 3
4 red 2 4
5 blue 4 1
6 blue 6 3
7 blue 2 -2
8 blue 6 -1
each line corresponds to a points (1,1 ; 1,3 etc...) , I just want to plot the ''red'' points in red , and the "blue" points in blue.
I know this is pretty dumb : but i just can't find a way to get a vector with the first four line.
I thought it was something like that:
plot(linearsep$color~x, linearsep$color~y)
but obviously it doesn't work ...
I've tested some stuff with ggplot:
ggplot(data=a,
+ aes(x=x, y=y, colour=color)) + geom_point()
Which works, but seems like a 'hack' , how can i just get the vector i want ?
Someone could please help me ... Again sorry for such a dumb question

Here's the complete call
p <- ggplot(foo, aes(x,y)) +
geom_point(aes(colour = color)) +
scale_colour_manual(values = c("red","blue"))
now you can do
p
or
print(p)
By assigning the ggplot to p, you can add more layers to later
by just doing p + ggtitle("Plot Title") for instance. This will be easier than
typing out everything again.
For getting only blue data or any other condition, you can subset and assign
it to new data.frame or do it within the ggplot call
ggplot(subset(a, colour == "blue"), aes(.....

In base R:
with(linearsep,plot(x,y,col=color,pch=16))
will give you two colors. If linearsep$color is a factor (which it probably is), then the colors will be red and black, because plot(...) uses the factor levels not the labels. You can get around this by converting the factor to character.
linearsep$color <- as.character(linearsep$color)
with(linearsep,plot(x,y,col=color,pch=16))
Now the colors will be red and blue.

Related

Plot frequency heatmap of positions from set of coordinates

I have a bunch of data that looks like this:
Track X1 X Y
1 Point 1 147.8333 258.5000
2 Point 2 148.5000 258.8333
3 Point 3 151.1667 260.8333
4 Point 4 154.5000 264.5000
5 Point 5 158.1667 266.5000
6 Point 6 161.5000 269.5000
I want to plot a heatmap of this, so a nice looking graph labelled x and y for the position coordinates, with a gradient color fill indicating the frequency that a particular point showed up, with a scale indicator showing what the colors mean. I'm looking for a simple gradient fill with a single color low and high.
I've been at this for a while but I think the first step should be to construct another data-set with the positions and a new column showing the frequencies? But I'm not 100% sure how to structure this.
So far my attempts look similar to:
ggplot(data=all_data, aes(x=X, y=Y)) + geom_tile(aes(fill=all_data$X)) +
scale_fill_gradient2(low="green", high="blue") + coord_equal()
As Jon Spring suggested, the following code shows up a graph like this:
all_data <- read.table(text = "
Track X1 X Y
1 Point 1 147.8333 258.5000
2 Point 2 148.5000 258.8333
3 Point 3 151.1667 260.8333
4 Point 4 154.5000 264.5000
5 Point 5 158.1667 266.5000
6 Point 6 161.5000 269.5000
", header = T, row.names = NULL)
ggplot(data=all_data, aes(x=X, y=Y)) + geom_bin2d()

Plotting curves with color gradient in R [duplicate]

This question already has answers here:
Continuous colour of geom_line according to y value
(3 answers)
Closed 4 years ago.
Might be a basic ggplot2 question, but I'm struggling a bit.
For example, I have a vector as follows:
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
As the numbers are decreasing from left to right, and then increasing from the middle, I want make a simple line curve(like the basic plot() function) for this vector. So for this, I would get something like an inverse bell.
But how do I set the line to be coloured, based on the value of the number?
The higher the number, the more the red the curve, and the more white for vice-versa, just like in a heatmap.
So for this specific vector I would want to plot an inverse bell, with the sides of the curves in colour red, the middle in colour white and the intermediates in gray.
Thanks in advance!
You can map the colour aesthetic to y. Below is an example using your color specification. I've used the default gray background so that the white portion of the line will be visible, though it would probably work better to use a higher-contrast color for the gradient:
library(ggplot2)
dat = data.frame(x=1:19, y=c(10:1,2:10))
ggplot(dat, aes(x,y, colour=y)) +
geom_line(size=1) +
scale_colour_gradient(low="white", high="red")
In the above example, the colour will be the same between each x value and will be set based on the x value at the left end of each segment. If you want the colour gradient to be interpolated between x values for smoother color transitions, you can plot an interpolated data frame:
ggplot(as.data.frame(approx(dat$x, dat$y, xout=seq(1,19,length=1000))),
aes(x,y, colour=y)) +
geom_line(size=1) +
scale_colour_gradient(low="white", high="red")
Define colors using the sapply. This example uses the predefined heat map.
values <- c(10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10)
heatcols <- heat.colors(10)
plot(values, xlab = '', ylab = '',
col = sapply(values, function(x) (heatcols[x])))
The color map can be anything you choose. Just use your choice of RGB values.
> heatcols
[1] "#FF0000FF" "#FF2400FF" "#FF4900FF" "#FF6D00FF" "#FF9200FF" "#FFB600FF" "#FFDB00FF" "#FFFF00FF"
[9] "#FFFF40FF" "#FFFFBFFF"

How to remove colored line on the x axis with geom_density from ggplot2?

Please, does anyone knows how to remove this colored line on the x axis? The dataframe is dfp.
Value Class
1 94.00 A
2 70.51 A
3 70.02 A
4 95.24 A
5 70.53 A
6 93.01 A
7 70.86 A
8 94.84 A
9 70.77 A
I´m using this command line to plot:
ggplot(dfp, aes(na.omit(dfp$Value), color = na.omit(dfp$Class), alpha=0.3) ) + geom_density(size=1.5)
ggplot(...) + stat_density(geom = "line")
The default geom is area, but you can change it to line. The benefit of the default is being able to shade the area (with a fill = aesthetic), rather than outline it.
You should use geom_freqpoly() instead of geom_density().
Best,
Colin

vertical line chart - change line plotting direction to top-down in R

I am looking for a way where data points are connected following a top-down manner to visualize a ranking. In that the y-axis represents the rank and the x-axis the attributes. With the normal setting the line connects the point starting from left to right. This results that the points are connected in the wrong order.
With the data below the line should be connected from (6,1) to (4,2) and then (5,3) etc. Optimally the ranking scale need to be inverted so that rank one starts on the top.
data <- read.table(header=TRUE, text='
attribute rank
1 6
2 5
3 4
4 2
5 3
6 1
7 7
8 11
9 10
10 8
11 9
')
plot(data$attribute,data$rank,type="l")
Is there a way to change the line drawing direction? My second idea would be to rotate the graph or maybe you have better ideas.
The graph I am trying to achieve is somewhat similar to this one:
example vertical line chart
You can do this with ggplot:
library(ggplot2)
ggplot(data, aes(y = attribute, x = rank)) +
geom_line() +
coord_flip() +
scale_x_reverse()
It solves the problem exactly the way you suggested. The first part of the command (ggplot(...) + geom_line()) creates an "ordinary" line plot. Note that I have already switched x- and y-coordinates. The next command (coord_flip()) flips x- and y-axis, and the last one (scale_x_reverse) changes the ordering of the x-axis (which is plotted as the y-axis) such that 1 is in the top left corner.
Just to show you that something like the example you linked in your question can be done with ggplot2, I add the following example:
library(tidyr)
data$attribute2 <- sample(data$attribute)
data$attribute3 <- sample(data$attribute)
plot_data <- pivot_longer(data, cols = -"rank")
ggplot(plot_data, aes(y = value, x = rank, colour = name)) +
geom_line() +
geom_point() +
coord_flip() +
scale_x_reverse()
If you intend to do your plots with R, learning ggplot2 is really worthwhile. You can find many examples on Cookbook for R.

How can I plot a image with (x,y,r,g,b) coordinates using ggplot2?

I have a data frame image.rgb, into which I have loaded the r,g,b value for each coordinate of an image (using the jpeg and reshape packages). It now looks like:
> head(image.rgb)
y x r g b
1 -1 1 0.1372549 0.1254902 0.1529412
2 -2 1 0.1372549 0.1176471 0.1411765
3 -3 1 0.1294118 0.1137255 0.1176471
4 -4 1 0.1254902 0.1254902 0.1254902
5 -5 1 0.1254902 0.1176471 0.1294118
6 -6 1 0.1725490 0.1372549 0.1176471
Now I want to plot this 'image' using ggplot2. I can plot a specific 'channel' (red or green or blue) one at a time using:
ggplot(data=image.rgb, aes(
x=x, y=y,
col=g) #green for example
) + geom_point()
...on the default ggplot2 colour scale
Is there a way to specify that the exact rgb values can be taken from the columns I specify?
Using the plot function in the base package, I can use
with(image.rgb, plot(x, y, col = rgb(r,g,b), asp = 1, pch = "."))
But I'd like to be able to do this using ggplot2
You must add scale_color_identity in order for colors to be taken "as is" :
ggplot(data=image.rgb, aes(x=x, y=y, col=rgb(r,g,b))) +
geom_point() +
scale_color_identity()
The sample data you provided give very similar colors, so all the points seem to be black. With geom_tile the different colors are a bit more visible :
ggplot(data=image.rgb, aes(x=x, y=y, fill=rgb(r,g,b))) +
geom_tile() +
scale_fill_identity()

Resources