I have some data that I want to plot (e.g. plot(x,y)) but I have some criteria that I want to color by depending on the values in vector z which looks like c(1, 0, 1, 1, 1, 1, 0, NA ,0 ,0, .....) etc.
Is there some way I can choose what color the 1's are going to be and what color the 0's are going to be?
Thanks
I know this has already been answered but here is some very intuitive code with a plot for reference.
#Let's create some fictional data
x = rbinom(100,1,.5)
x[round(runif(10)*100)] = NA
#Assign colors to 1's and 0's
colors = rep(NA,length(x))
colors[x==1] = "blue"
colors[x==0] = "red"
#Plot the vector x
plot(x,bg=colors,pch=21)
You want to get a vector of colors as long as the x and y vectors, for example as follows:
z[is.na(z)] = 2
zcol = c("red", "blue", "black")[z + 1]
Then you can simply do:
plot(x, y, col=zcol)
Maybe I am missing something, but I think you want:
plot(x,y,col=ifelse(z==0,'zerocolour','onecolour'))
where you replace the two colours with red and blue or whatever.
I don't think the NA will be plotted, so you don't have to worry about those.
For more colours, you could create a little mapping data.frame with the unique values of z, and then merge z with the data.frame. Here is an example with two colours:
map<-data.frame(z=c(0,1),col=c('red','blue'))
plot(x,y,col=merge(z,map)$col)
Using the ggplot2 package
require(ggplot2)
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(2, 3, 4, 6, 7), z = c(1, 0 , 1, 0 , NA))
df$z[is.na(df$z)] = 2
ggplot(df, aes(x, y, color = as.factor(z))) + geom_point()
You can supply vector of colors to argument col= and then use z to select colors. Used paste() to convert NA to character and then as.factor() to interpret those characters as 1, 2 and 3.
x<-c(1,2,3,4,5)
y<-c(1,2,3,4,5)
z<-c(1,0,NA,1,1)
plot(x,y,col=c("red","green",'black')[as.factor(paste(z))],pch=19,cex=3)
str(as.factor(paste(z)))
Factor w/ 3 levels "0","1","NA": 2 1 3 2 2
Related
I did made three pie3D (plotrix) pictures in R.
1 with the population as a whole
1 for the adults of the population
1 for children of the population
Now, it are three separate figures. Is it possible to add them together to 1 figure?
ggarange will not work because pie3D is not a ggplot function.
Does anybody knows other options?
In R base plotting you can set some graphical parameters with par to determine the output. Type ?par in the console to see the help file. The following is an excerpt from the helpfile:
mfcol, mfrow
A vector of the form c(nr, nc). Subsequent figures will be drawn in an nr-by-nc array on the device by columns (mfcol), or rows (mfrow), respectively.
With the following code and mockup data, you can arrange the three plots in a row of 3 "columns"
library(plotrix)
## some mocked up data
x1 <- c(4, 5, 9, 3, 6)
x2 <- c(7, 2, 1, 1, 4)
x3 <- c(5, 5, 2, 1, 1)
## some common labels
labels <- c("a", "b", "c", "d", "e")
## set to 1 row, 3 columns
par(mfrow = c(1, 3))
pie3D(x1, labels = labels, explode = .1)
pie3D(x2, labels = labels, explode = .1)
pie3D(x3, labels = labels, explode = .1)
## re-set to 1 row, 1 column
par(mfrow = c(1, 1))
yielding this graph:
Please let me know whether this is what you want.
I want to create a barchart of some data, I collected from a survey using the R programming language. The X values of my data range between 4-10 but I want my xaxis to range between 0-10 to display the full range of variables that my survey could have possibly given.
Data:
X = 4 5 6 7 8 9 10
Y = 5 7 2 10 5 2 5
I have tried:
barplot(data, xlim(0,10),xpd=TRUE)
I've also tried using the 'expand_limits' function in ggplot2 with no avail either.
This hasnt done what I expected. I want my graph to range between 0 and 10 with bars only plotted on x values of 4-10.
If someone could help me I'd be really thankful.
Welcome to SO!
The barplot function doesn't really have an "x-axis", as you can see. If you try using the limits, it will assume that the first height you give it is the height number 1 and so on, and put the "empty space" at the end.
Alternatively, this ggplot2 solution should work:
library(ggplot2)
ggplot(df, aes(X, Y)) +
geom_col(color = "black", fill = "grey60") +
lims(x = c(0,10.5)) +
theme_classic()
You can read more about it to tweak the plot style.
A barplot is really for categorical variables, so if you convert your X variable to a factor and include all the levels you want, you should get the desired plot:
data <- data.frame(X = factor(c(4, 5, 6, 7, 8, 9, 10), levels = 1:10),
Y = c(5, 7, 2, 10, 5, 2, 5))
barplot(Y ~ X, data = data, xpd=TRUE)
Created on 2020-06-29 by the reprex package (v0.3.0)
While plotting a boxplot in R, I noticed not all values in the y-axis are presented. Possible values are -5 to 5, but actual values are -1.3 to 4.6, so the values presented on the y-axis are -2 to 5. I want it to be presented with all values: -5 to 5, even though there's no data for this entire range.
My code looks like this:
boxplot(depvar ~ indepvar, data = a, pars = list(outlwd = 2, outcex = 1.8), axes = FALSE)
axis(side = 2, at = seq(-5, 5, by = 1), las = 1, tck = 7)
What should be added/changed for the y-axis to be fully-presented?
Appears simliar to this question: How to set the y range in boxplot graph?
I think you are looking for ylim.
a <- c((randu$x*3)-2)
boxplot(x = a,
ylim = c(-5,5))
Load Packages
install.packages("dplyr")
library(dplyr)
creating random set with two columns:
set.seed(10)
df <- dplyr::data_frame(
x = 1:5,
y = 1:5)
Visualize in a boxplot with expanded axis:
boxplot(x~y,
df,
xlim =c(-5,5),
ylim =c(-5,5))
My data looks something like this:
x = A, A, B, C, D, E, E, F, G
y = 1, 0, 2, 3, 4, 5, 0, 6, 7
How do I have every alternate x-axis data point a different colour? For example, in my data, I would like to have the A, C, E and G points one colour, B, D, F another and then have the points where the value is 0 a third colour? I've used pch = and col= to change the colour and shape of everything, but is it possible to change specific points?
Or would I have to plot two different graphs on the same plot, and then change the colour of each graph, then have a third rule for the zeroes? The only problem with this is that since my variables are not numerical, and the data needs to be alternating, I can't seem to figure out how to plot it.
If this is your data:
x = c('A', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
y = c(1, 0, 2, 3, 4, 5, 0, 6, 0)
Then decide on the three colours,
cols <- c("orange2","green4","tomato3")
And create the colour vector based on your criteria. You can use ifelse to determine the index to use for the cols vector.
col <- cols[ifelse(x %in% c('A','C','E','G'), 1, # orange
ifelse(x %in% c('B','D','F'), 2, # green
ifelse(y==0, 3, # tomato
4)))] # no 4th colour, so NA (the points won't appear)
col
# "orange2" "orange2" "green4" "orange2" "green4" "orange2" "green4" "orange2" "tomato3"
The length of col is the same as your data, and because R vectorises, everything works.
plot(1:length(y), y, xaxt="n", las=1, xlab="x", col=col, pch=20, cex=3)
axis(side=1, at=1:length(y), labels=x)
It is of course possible in base R plot, I would do it using factor for your grouping variable, you wouldn't even need to give R specific colour values, because it would do it automatic for factors :
x = 1:10
y = sample(1:20, 10, replace= T)
group = factor(sample(LETTERS[1:9],10, replace= T ))
df<-data.frame(x,y,group)
#> head(df)
# x y group
#1 1 16 B
#2 2 7 D
#3 3 10 D
#4 4 19 I
#5 5 3 C
#6 6 13 I
plot(x,y, col=group, pch=20, cex= 2)
I would like to make a simple line plot like this:
things <- c(1, 3, 6, 4, 9)
plot(things, type="o", col="blue", axes=FALSE, ann=FALSE)
axis(1, at=1:5, lab=c("Mon","Tue","Wed","Thu","Fri"))
axis(2, las=1)
box()
(Image)
BUT with the single line changing color at a certain data point, in this case, say, blue Monday-Wednesay, and red for Wednesday-Friday. I.e. from the data point number 1 to 3, the line is blue, from 3 to 5, it would be red.
I know I can just split the data series into two, and plot them separately and the image will join them, but the real data I am using is from a large complex data frame, and I need to make the plot from dozens of them, so having one quick little code to do it without manipulating the actual data would be a big time-saver.
One line, two colors, that's it!
Thanks!
Maybe I'm misunderstanding what you need here, but it seems to me that you can do it easily enough in ggplot2.
library(ggplot2)
dd <- data.frame(days = c("Mon","Tue","Wed","Thu","Fri"),
things = c(1, 3, 6, 4, 9))
# set the levels of the factor so that 'days' sorts properly
dd$days <- factor(dd$days, levels = c('Mon','Tue','Wed','Thu','Fri'))
# which days do we want to highlight?
days.highlight <- dd$days[4:5]
dd$highlight <- ifelse(dd$days %in% days.highlight, "red", "black")
ggplot(dd, aes(x = days, y = things, colour = highlight, group = 1)) +
geom_line() +
geom_point() +
scale_colour_identity(dd$highlight)