[R] question about R graphics-example plot attached
Marc Schwartz
MSchwartz at mn.rr.com
Tue Nov 8 15:03:11 CET 2005
On Tue, 2005-11-08 at 14:08 +0100, jia ding wrote:
> ---------- Forwarded message ----------
> From: jia ding <dingjia at gmail.com>
> Date: Nov 2, 2005 4:03 PM
> Subject: question about R graphics-example plot attached
> To: r-help at lists.r-project.org
>
> Suppose I have the data set like this:
> A 1
> 3
> 7
> 10
>
> B 5
> 9
> 13
> The numbers here actually is A or B's occurence positions.So it means,
> position 1,3,7,10 is A;position 5,9,13 is B's occurence.
>
> I want the plot is a line with a little dot (or bar) at the position
> 1(A-show red), position 3(A-red),position
> 5(B-blue),7(A-red),10(A-red),13(B-blue)
>
> I am not sure, If I explained very clearly about my question. What a
> pity google group not support to attach a file. Otherwise,I can provide
> an example.
>
> Thanks for the help!
> Thanks Marc.
> DJ
DJ,
In follow up to your post to sci.stat.math and your e-mail to me, let me
put forth an alternative to the solution that I initially posted to
sci.stat.math.
First, let's set up the data again and repeat (for the sake of readers
here) my initial solution, followed by a second option, given your
example plot.
# Create a data frame with your data as above.
# Use the I() construct to keep the colors as
# characters, rather than factors
data <- data.frame(pos = c(1, 3, 7, 10, 5, 9, 13),
col = I(c(rep("red", 4),
rep("blue", 3))))
# Data now looks like this:
> data
pos col
1 1 red
2 3 red
3 7 red
4 10 red
5 5 blue
6 9 blue
7 13 blue
# We need to sort the rows by the position number
# for plotting in sequence
data <- data[order(data$pos), ]
# Data now looks like this:
> data
pos col
1 1 red
2 3 red
5 5 blue
3 7 red
6 9 blue
4 10 red
7 13 blue
# Now generate the plot, using the 'pos' values
# for the x axis and fix the y axis at 1
# Also set the plot type for both lines and points
# The colors for the points will be set in sequence
# Do not plot any annotation or axis values
plot(data$pos, rep(1, 7), type = "b",
bg = data$col, pch = 21, ann = FALSE,
axes = FALSE)
# Now create the x axis with tick marks and labels
# at the 'pos' values
axis(1, at = data$pos)
So that generates a plot using colored points with connecting lines. Now
seeing your example, where I took the word "line" to mean one connecting
the points, I see now you are referring to the x axis.
Here is the second option:
First start with the same approach to configuring your data so that you
end up with 'data' as per above. Now, we'll use plot() with type 'h',
which creates vertical lines. We will set the line widths (lwd) wider so
that they appear more as bars and adjust the line ends so that they are
square rather than round.
# Set line ends to square
par(lend = 2)
# Use the same approach as above, but type = 'h'
# and set line width to be fairly wide
plot(data$pos, rep(1, 7), type = "h", col = data$col, lwd = 20,
ann = FALSE, axes = FALSE)
# Now do the x axis at data values
axis(1, at = data$pos)
Does that get you closer to what you intended?
HTH,
Marc Schwartz
More information about the R-help
mailing list