The following code :
avector <- as.vector(top.links.added.overall$Amount)
x <- as.vector(top.links.added.overall[order(avector),])
row.names(x) <- c("Yahoo" ,"Cnn", "Google")
x$color[x$Amount == 100] <- "red"
x$color[x$Amount == 500] <- "blue"
x$color[x$Amount == 1000] <- "darkgreen"
dotchart(x$Amount,
labels = row.names(x),
cex=.7,
groups = x$Amount,
gcolor = "black",
color = x$color,
pch=19,
main = "Gas Mileage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
Generates this graph :
Here is the format of the dataset top.links.added.overall$Amount :
here is the file dataset :
Amount,Name
1000,Google
500,Cnn
100,Yahoo
When I remove the code :
row.names(x) <- c("Yahoo" ,"Cnn", "Google")
I get row names of 1,2,3
I don't need I should need to set the names of the 'y' axis ? How can the code of the graph be amended so that the company with lowest numerical value(in this case yahoo) start at beginning of 'y' axis instead of top, which is currently what is occuring ?
I don't think I can test it with the offered R data objects but perhaps something along these lines:
x <- as.vector(top.links.added.overall[order(-avector),])
row.names(x) <- rev( c("Yahoo" ,"Cnn", "Google") )
Using mathematical negation to the order argument and the rev (reverse) function.
Edit: I now understand your frustration, but after looking at the code I decided to try this which seems to do it:
dotchart(x$Amount,
labels = row.names(x),
cex=.7,
groups = -x$Amount, # the code sorts by `as.numeric(groups)`
gcolor = "black",
color = x$color,
pch=19,
main = "Gas Mileage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
Related
Below is the code.
stripchart(Age~Smoke, data = survey_clean_data , pch=16 , col = "blue", method = "jitter" ,main = "AGE VS SMOKE",na.rm = T)
I want to add labels to it like below image,
I tried several options.. but it is getting written on top of of other.
means = c(paste("mean_Age =",roumean(survey_clean_data[Smoke == "Heavy","Age"],na.rm =T)),
paste("mean_Age =",mean(survey_clean_data[Smoke == "Never","Age"],na.rm =T)),
paste("mean_Age =",mean(survey_clean_data[Smoke == "Regul","Age"],na.rm =T)),
paste("mean_Age =",mean(survey_clean_data[Smoke == "Occas","Age"],na.rm =T)))
text(50,survey_clean_data$Smoke,labels = means)
DATA: library(MASS) attach(survey)
There are a few problems with your code. The main thing is that you are sending text() four labels (the contents of means), but a number of y-coordinates equal to the number of data points, since you are sending it survey_clean_data. R tries to equalize these uneven vectors, resulting in the over plotting.
Instead, you might do (data are artificial since you didn't provide any):
stripchart(Age~Smoke, data = survey_clean_data , pch=16 , col = "blue", method = "jitter" ,main = "AGE VS SMOKE",na.rm = T)
means <- aggregate(Age~Smoke, data = survey_clean_data, FUN = mean) # mean of each category
means$y <- 1:4 # add y-coordinates for each category
with(means, text(50, Smoke, labels = sprintf('Mean Age = %0.1f', Age))) # plot text labels on top of stripchart
Result:
answer give by jdobres worked fine. The below is one more solution.
add ylim=c(0.8,4.2) parameter to the scatterplot. You can adjust these ranges from c(1,4) to c(0.8,4.2). The later one worked for me.
stripchart(Age~Smoke, data = survey_clean_data , pch=16 , col = 634, method = "jitter" ,main = "AGE VS SMOKE",na.rm = T,ylim=c(0.8,4.2))
With the below line you can adjust the vertical height of the text.
eg: +0.1, -0.1 etc
text(50,c(1:4)+0.1,means)
I have just started with R and now I am trying to plot the amount of months with information per year in each station by using matrixplot function as following:
a <- dwi(Monthly_data[,1:54], out.unit = "years", dates=1)
matrixplot(a, var.type ="Days", main="Number of months with info per year")
and I get the plot with the scale, on the color bar, from 2-12.
enter image description here
I want to change the scale on the colorbar to 0-12. I tried with xlim and ylim but they both don't work on the colorbar. Please give me any suggestion. Thank you.
I hope this is what you are looking for:
require(hydroTSM)
data(EbroPPtsMonthly)
Monthly_data <- EbroPPtsMonthly
a <- dwi(Monthly_data[,1:54], out.unit = "years", dates=1)
cols = colorRampPalette(c("red", "green"))
matrixplot(
x = a
, main="Number of months with info per year"
, ColorRamp = cols
, at = c(0,1,2,3,4,5,6,7,8,9,10,11,12)
, colorkey = list(
at=seq(from=0, to=12, by=1)
, labels = c("0","1","2","3","4","5","6","7","8","9","10","11","12")
, col = cols
)
)
Let's continue counting rain drops... 555
So I am trying to add some graphs to my notes. I have created a simple interest function that will plot several simple interest functions using different rates and I would like to add a legend that would simple say...
"i =: 0%, x%, y%, z%" on one single line, where each 0,x,y,z is in the different color of the representative function using that interest rate.
I looked into the paste() function and attempted to make it one string but I am not sure exactly how to loop it into the int_seq and pull out each individual index and make it a different color then put it into a single string.
# indexs to be used
t = 0:50
int_seq = seq(0.025,0.10,by=0.025) # intere rate sequence
colors = c("red","blue","green","orange") #colors of interest rate seq
index = 1:length(int_seq)
# AV Simple Interest (all good)
avSimple = function(i,t){
av = (1 + (i * t))
return(av)}
# Plot range for y-axis (all good)
yrange = c(avSimple(min(int_seq),min(t)) * 0.95,
avSimple(max(int_seq),max(t)) * 1.05)
# Plots Simple Interest with different interest rates (all good)
plot(t,avSimple(0,t), type="l", main = "AV Simple Interest", xlab = "Time",
ylab = "AV", ylim = yrange)
# loops through the int_seq and plots line based on interest rate
# and specified color (all good)
for (i in index)
lines(t,avSimple(int_seq[i],t), col = colors[i])
# Adds legend to plot for different interest rates
# !!This is where I need the help, not sure best way to approach!!
legend(0,avSimple(0.075,50), c("i =: 0%", for (i in index) int_seq[i]),
col = colors)
Not sure what kind of legend you want. Since you say in one line, you might want to add horiz = TRUE, but here are some other options:
You can pass full vectors to legend so there is no need for a loop in this case. Just create a vector of labels but also use a vector of colors corresponding to each label (which you have already done).
# indexs to be used
t = 0:50
int_seq = seq(0.025,0.10,by=0.025) # intere rate sequence
colors = c("red","blue","green","orange") #colors of interest rate seq
index = 1:length(int_seq)
# AV Simple Interest (all good)
avSimple = function(i,t){
av = (1 + (i * t))
return(av)}
# Plot range for y-axis (all good)
yrange = c(avSimple(min(int_seq),min(t)) * 0.95,
avSimple(max(int_seq),max(t)) * 1.05)
plot(t, type="n", main = "AV Simple Interest", xlab = "Time",
ylab = "AV", ylim = yrange)
# for (i in index)
# lines(t,avSimple(int_seq[i],t), col = colors[i])
# Adds legend to plot for different interest rates
# !!This is where I need the help, not sure best way to approach!!
labs <- sprintf('i =: %s%%', c(0, int_seq))
labs2 <- paste0(c(0, int_seq), '%')
legend('topleft', legend = labs, col = colors, lty = 1, title = 'normal')
l <- legend('top', legend = rep('i =:', length(labs)), lty = 1,
col = colors, text.width = max(strwidth(labs)) + 1,
title = 'right-justified')
text(l$rect$left + l$rect$w, l$text$y, labs2, pos = 2)
legend('topright', legend = labs, text.col = colors, title = 'colored')
legend('bottom', legend = labs, col = colors, lty = 1, horiz = TRUE,
cex = .7, title = 'horizontal')
This R code :
avector <- as.vector(top.links.added.overall$Amount)
x <- as.vector(top.links.added.overall[order(avector),])
x$Amount <- factor(x$Amount)
x$color[x$Amount == 100] <- "red"
x$color[x$Amount == 500] <- "blue"
x$color[x$Amount == 1000] <- "darkgreen"
dotchart(x$Amount,
labels = row.names(x),
cex=.7,
groups = x$Amount,
gcolor = "black",
color = x$color,
pch=19,
main = "Gas Mileage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
returns this error :
Error in dotchart(x$Amount, labels = row.names(x), cex = 0.7, groups = x$Amount, :
'x' must be a numeric vector or matrix
This is the datafile for top.links.added.overall :
Amount,Name
1000,Google
500,Cnn
100,Yahoo
'x' is a vector so what is causing this error ?
Remove conversion to factor x$Amount <- factor(x$Amount)
And make small change in
dotchart(x$Amount,
labels = row.names(x),
cex=.7,
groups = factor(x$Amount),
gcolor = "black",
color = x$color,
pch=19,
main = "Gas Mileage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
Probably that will help you.
I have following data and plot:
pos <- rep(1:2000, 20)
xv =c(rep(1:20, each = 2000))
# colrs <- unique(xv)
colrs <- xv # edits
yv =rnorm(2000*20, 0.5, 0.1)
xv = lapply(unique(xv), function(x) pos[xv==x])
to.add = cumsum(sapply(xv, max) + 1000)
bp <- c(xv[[1]], unlist(lapply(2:length(xv), function(x) xv[[x]] + to.add[x-1])))
plot (bp,yv, pch = "*", col = colrs)
I have few issues in this plot I could not figure out.
(1) I want to use different color for different group or two different color for different groups (i.e xv), but when I tried color function in terms to be beautiful mixture. Although I need to highlight some points (for example bp 4000 to 4500 for example with blue color)
(2) Instead of bp positions I want to put a tick mark and label with the group.
Thank you, appreciate your help.
Edits: with help of the following answer (with slight different approach in case I have unbalanced number in each group will work) I could get the similar plot. But still question remaining regarding colors is what if I want to use two alternate colors in alternate group ?
You can solve your colour issue by repeating the colour index however many times each group has a point plotted, like so:
plot (bp,yv, pch = "*", col = rep(colrs,each=2000))
The default colour palette (see ?palette or palette() ) will wrap around itself and you might want to specify your own to get 20 distinct colours.
To relabel the x axis, try plotting without the axis and then specifying the points and labels manually.
plot (bp,yv, pch = "*", col = rep(colrs,each=2000),xaxt="n")
axis(1,at=seq(1000,58000,3000),labels=1:20)
If you are trying to squeeze a lot of labels in there, you might have to shrink the text (cex.axis)or spin the labels 90 degrees (las=2).
plot (bp,yv, pch = "*", col = rep(colrs,each=2000),xaxt="n")
axis(1,at=seq(1000,58000,3000),labels=1:20,cex.axis=0.7,las=2)
Result:
One way is you could use a nested ifelse.
I'm still learning R, but one way it could be done would look something like:
plot(whatev$x, whatev$y, col=ifelse(xv<2000,red,ifelse(2000<xv & xv<4000,yellow,blue)))
You could nest as many of these as you want to have specificity on the colors and the intervals. The ifelse command is of form ifelse(TEST, True, False).
A simpler way would be to use the unique groups in xv to assign rainbow colors.
colrs=rainbow(length(unique(xv))) #Or colrs=rainbow(length(xv)) if xv is unique.
plot(whatev$x, whatev$y, col=colrs)
I hope I got all that right. I'm still learning R myself.
I'm going to go out on a limb and guess that your real data are something like 2000 values of things from 20 different groups. For instance, heights of 2000 plants of 20 different species. In such a case, you might want to look at the dotplot() function (or as illustrated below, dotplot.table()) in the lattice package.
Generate matrix of hypothetical values:
set.seed(1)
myY <- sapply( seq_len(20), function(x) rnorm(2000, x^(1/3)))
Transpose matrix to get groups as rows
myY <- t(myY)
Provide names of groups to matrix:
dimnames(myY)[[1]]<-paste("group", seq_len(nrow(myY)))
Load lattice package
library(lattice)
Generate dotplot
dotplot(myY, horizontal = FALSE, panel = function(x, y, horizontal, ...) {
panel.dotplot(x = x, y = y, horizontal = horizontal, jitter.x = TRUE,
col = seq_len(20)[x], pch = "*", cex = 1.5)
}, scales = list(x = list(rot = 90))
)
Which looks like (with unfortunate y-axis labeling):
Seeing that #JohnCLK is requesting a way of colouring by values on the x axis, I tried these demos in ggplot2-- each uses a dummy variable that is coded based on values or ranges to be highlighted in the other variables.
So, first set up the data, as in the question:
pos <- rep(1:2000, 20)
xv <- c(rep(1:20, each = 2000))
yv <- (2000*20, 0.5, 0.1)
xv <- lapply(unique(xv), function(x) pos[xv==x])
to.add <- cumsum(sapply(xv, max) + 1000)
bp <- c(xv[[1]], unlist(lapply(2:length(xv), function(x) xv[[x]] + to.add[x-1])))
Then load ggplot2, prepare a couple of utility functions, and set the default theme:
library("ggplot2")
make.png <- function(p, fName) {
png(fName, width=640, height=480, units="px")
print(p)
dev.off()
}
make.plot <- function(df) {
p <- ggplot(df,
aes(x = bp,
y = yv,
colour = highlight))
p <- p + geom_point()
p <- p + opts(legend.position = "none")
return(p)
}
theme_set( theme_bw() )
Draw a plot which highlights values in a defined range on the vertical axis:
# highlight a horizontal band
df <- data.frame(cbind(bp, yv))
df$highlight <- 0
df$highlight[ df$yv >= 0.4 & df$yv < 0.45 ] <- 1
p <- make.plot(df)
print(p)
make.png(p, "demo_horizontal.png")
Next draw a plot which highlights values in a defined range on the x axis, a vertical band:
# highlight a vertical band
df$highlight <- 0
df$highlight[ df$bp >= 38000 & df$bp < 42000 ] <- 1
p <- make.plot(df)
print(p)
make.png(p, "demo_vertical.png")
And finally draw a plot which highlights alternating vertical bands, by x value:
# highlight alternating bands
library("gtools")
alt.band.width <- 2000
df$highlight <- as.integer(df$bp / alt.band.width)
df$highlight <- ifelse(odd(df$highlight), 1, 0)
p <- make.plot(df)
print(p)
make.png(p, "demo_alternating.png")
Hope this helps; it was good practice anyway.