I'm really new to R and I'm trying to convert a 2 column table into an xy-Plot.
Here's my .csv:
x [cm];y [cm]
0.5;0
2.6;9
0.5;1
0.6;2
0.7;3
0.8;4
1;5
1.2;6
1.5;7
1.9;8
Now: plot(data$`x [cm]`,data$`y [cm]`, type="b").
However I get this result:
I'm not quite sure why (0.5/y) and (2.6/y) are connected..
What I want is a simple line connecting all the dots since they are representing electric field lines. Is there an easy way of doing that?
Sort your data first:
data <- data[order(data[,1]),]
plot(data[,1], data[,2], type="b", xlab="x [cm]", ylab="y [cm]")
The points are connected like this because the connection is created based on their order in the matrix.
m <- matrix(c(
0.5, 0,
0.5, 1,
0.6, 2,
0.7, 3,
0.8, 4,
1, 5,
1.2, 6,
1.5, 7,
1.9, 8,
2.6, 9), ncol = 2, byrow = TRUE)
colnames(m) <- c("x", "y")
plot(m, type = "b")
Simly regrouping the matrix solves your problem.
You can use
library(ggplot2)
ggplot(data, aes(x=`x [cm]`, y=`y [cm]`)) + geom_point() + geom_line()
Or using base R plot
plot(data$`x [cm]`, data$`y [cm]`,
xlim=range(data$`x [cm]`), ylim=range(data$`y [cm]`),
xlab="x [cm]", ylab="y [cm]")
lines(data$`x [cm]`[order(data$`x [cm]`)], data$`y [cm]`[order(data$`y [cm]`)],
xlim=range(data$`x [cm]`), ylim=range(data$`y [cm]`))
Related
I cannot figure out how to get the percentage of responses at the end of the bars. I know I'm missing something within the text() function, just not sure what exactly I'm missing. Thank you!
#Training/Specialty Barplot
trainbarplot <- barplot(table(PSR$training), horiz = TRUE,
main="Respondent Distribution of Training", cex.main = 1.1, font.main = 2,
cex.lab = 0.8, cex.names = 0.4, font.axis = 4, las = 2,
xlab="Response Frequency", xlim=c(0, 40), cex.axis = 0.8,
border="black",
col=rgb (0.1, 0.1, 0.4, 0.5, 0.6),
density=c(50,40,30) , angle=c(9,11,36)
)
text(trainbarplot, table(PSR$training) - 3,
labels=paste(round(proportions(table(PSR$training))*100, 0), "%"))
Generate data
I generated some sample data to replicate your problem. Please note that you should always try to provide an example dataset :)
set.seed(123)
df1 <- data.frame(x = rnorm(10, mean=10, sd=2), y = LETTERS[1:20])
Plot the data
Here's a plot that follows the same structure as your code:
bp <- barplot(df1$x, names.arg = df1$y, col = df1$colour, horiz = T)
text(x= df1$x+0.5, y= bp, labels=paste0(round(df1$x),"%"), xpd=TRUE)
Using ggplot2
You can also plot your data using ggplot2. For instance, you could first create a new column in your dataset with information on the labels...
df1$perc <- paste0(round(df1$x),"%")
Next, you can plot your data using ggplot and adding different relevant layers.
library(ggplot2)
ggplot(df1, aes(x = x, y = y)) +
geom_col() +
geom_text(aes(label = perc)) +
theme_minimal()
Good luck!
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))
I want to overlay a plot of an empirical cdf with a cdf of a normal distribution. I can only get the code to work without using ggplot.
rnd_nv1 <- rnorm(1000, 1.5, 0.5)
plot(ecdf(rnd_nv1))
lines(seq(0, 3, by=.1), pnorm(seq(0, 3, by=.1), 1.5, 0.5), col=2)
For ggplot to work I would need a single data frame, for example joining rnd_vn1 and pnorm(seq(0, 3, by=.1), 1.5, 0.5), col=2). This is a problem, because the function rnorm gives me just the function values without values on the domain. I don't even know how rnorm creates these, if I view the table I just see function values. But then again, magically, the plot of rnd_nv1 works.
The following plots the two lines but they overlap, since they are almost equal.
set.seed(1856)
x <- seq(0, 3, by = 0.1)
rnd_nv1 <- rnorm(1000, 1.5, 0.5)
dat <- data.frame(x = x, ecdf = ecdf(rnd_nv1)(x), norm = pnorm(x, 1.5, 0.5))
library(ggplot2)
long <- reshape2::melt(dat, id.vars = "x")
ggplot(long, aes(x = x, y = value, colour = variable)) +
geom_line()
I want to format my x-axis in log2(n+1) format so the x-axis labels correspond to 1, 2, 4, 16 and so on.
Input:
x <- c(1, 2, 3, 11, 15)
y <- c(1.1, 1.2, .4, 2.1, 1.5)
plot(log2(x + 1), y, axes=FALSE)
axis(1, at=(labels=as.character(formatC(x))), cex.axis=0.9)
But plot I get still has the original x-axis values.
How can I make my x-axis powers of 2 (1, 2, 4, 16, etc.)?
I guess this is what you want.
x<-c(1,2,3,11,15)
y<-c(1.1,1.2,.4,2.1,1.5)
lab<-c(1,2,4,16)
plot(log2(x+1),y,xaxt="n",xlab="x")
axis(1,at=log2(lab+1),labels=lab)
It might also be useful to calculate equally spaced labels:
lab<-round(2^seq(min(log2(x+1)),max(log2(x+1)),length.out=4)-1)
I'm looking to plot something similar to this in R. Can this be this be done with ggplot or some other package?
Found on the following blog:
http://intelligenttradingtech.blogspot.com/2011/07/pattern-recognition-forward-boxplot.html
Here is how to construct the graph using ggplot2.
I constructed the data manually, by specifying the coordinates of each line start and end position. An improvement would obviously be to automate this using an algorithm. Since this wasn't the question, I didn't attempt to solve this too.
Create the data:
arrowdata <- c(
0, 0, 1, 0,
1, 1, 2, 1,
1, -1, 2, -1,
2, 1.5, 3, 1.5,
2, 0.5, 3, 0.5
)
linesdata <- c(
1, 0, 1, 1,
1, 0, 1, -1,
2, 1, 2, 1.5,
2, 1, 2, 0.5
)
labeldata <- data.frame(
x = c(0.5, 1.5, 2.5),
y = c(0, 1, 1.5),
labels=c("Label 1", "Label2", "Label 3")
)
adat <- as.data.frame(matrix(arrowdata, ncol=4, byrow=TRUE))
ldat <- as.data.frame(matrix(linesdata, ncol=4, byrow=TRUE))
Load the ggplot2 and grid packages, then plot:
library(ggplot2)
library(grid) # For arrow() function
ggplot() +
geom_segment(
data=adat,
aes(x=V1, y=V2, xend=V3, yend=V4),
arrow=arrow(length = unit(0.05, "npc"), type="closed"),
col="blue"
) +
geom_segment(
data=ldat,
aes(x=V1, y=V2, xend=V3, yend=V4),
col="blue"
) +
geom_text(data=labeldata, aes(x, y, label=labels),
size=8, vjust=-0.2, col="blue"
) +
theme_bw() +
opts(
axis.text.x=theme_blank(),
axis.text.y=theme_blank(),
axis.ticks=theme_blank(),
axis.title.x=theme_blank(),
axis.title.y=theme_blank(),
panel.grid.major=theme_blank(),
panel.border=theme_blank()
) +
coord_cartesian(ylim=c(-1.5, 2)) # Create some additional space for labels
You might find something at http://addictedtor.free.fr/graphiques/ . There's an amazing variety of graphs and charts there. Now, it's easy enough to write a little code using the base plot and graphics::arrow functions that will draw lines between the vertices. E.g.,
arrows(0,1,0,0)
lines(c(1,1),c(-.5,.5))
arrows(1,2,.5,.5)
and so on. Do you have a requirement to size or place the branches based on data, or is this a purely qualitative tree?