How to get ggplot2 quick reference?

93 views
Skip to first unread message

Jinyan Huang

unread,
Mar 11, 2013, 3:35:43 PM3/11/13
to ggp...@googlegroups.com
Dear list,

I am enjoying to use ggplot2.

But sometimes I found it is a bit of confused.

For example, I just want to draw a segment. I use geom_segment.

How can I know what kind of "linetype" I can use? Are there a full
list for "linetype" ?

Why this is not working?

geom_segment(aes(x = 10, y = 0, xend = 10, yend =
300,col="red",linetype="dashed"))

But this is OK.
geom_segment(aes(x = 10, y = 0, xend = 10, yend =
300,col="red"),linetype="dashed")

Thank you.

Winston Chang

unread,
Mar 11, 2013, 3:40:12 PM3/11/13
to Jinyan Huang, ggp...@googlegroups.com
See:
  http://www.cookbook-r.com/Graphs/Shapes_and_line_types/

Also, for drawing a single segment (as opposed to drawing segments based on values from a data frame), you probably want to use annotate("segment") instead of geom_segment(). The latter will draw one segment for each row in your data frame. See the annotate() help page for examples:

-Winston


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Ista Zahn

unread,
Mar 11, 2013, 3:43:21 PM3/11/13
to Jinyan Huang, ggp...@googlegroups.com
Hi,

On Mon, Mar 11, 2013 at 3:35 PM, Jinyan Huang <jhu...@hsph.harvard.edu> wrote:
> Dear list,
>
> I am enjoying to use ggplot2.
>
> But sometimes I found it is a bit of confused.
>
> For example, I just want to draw a segment. I use geom_segment.
>
> How can I know what kind of "linetype" I can use? Are there a full
> list for "linetype" ?

?aes says

"See Also:

'aes_string' for passing quoted variable names.
'aes_colour_fill_alpha', 'aes_group_order',
'aes_linetype_size_shape' and 'aes_position' for more specific
examples with different aesthetics."

?aes_linetype_size_shape says

" # Line types should be specified with either an integer, a name,
or with a string of
# an even number (up to eight) of hexidecimal digits which give
the lengths in
# consecutive positions in the string.
# 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 =
longdash, 6 = twodash"

>
> Why this is not working?
>
> geom_segment(aes(x = 10, y = 0, xend = 10, yend =
> 300,col="red",linetype="dashed"))

aes is for mapping variables to aesthetics. "dashed" is not a
variable, but a constant. In this case you should set it outside of
the aes call, as you did below.

HTH,
Ista

>
> But this is OK.
> geom_segment(aes(x = 10, y = 0, xend = 10, yend =
> 300,col="red"),linetype="dashed")
>
> Thank you.
>

Jinyan Huang

unread,
Mar 11, 2013, 4:02:31 PM3/11/13
to Ista Zahn, ggp...@googlegroups.com
Ista,

Thank you for your explain. Your first part answers make sense to me.
Later part still confused me.

geom_segment(aes(x = 10, y = 0, xend = 10, yend =
300,col="red",linetype="dashed"))

aes is for mapping variables to aesthetics. But here 10, 0, 10, 300,
"red" (x = 10, y = 0, xend = 10, yend = 300,col="red") should also be
constant. How can I understand this trick?

On Mon, Mar 11, 2013 at 3:43 PM, Ista Zahn <ista...@gmail.com> wrote:
> Hi,
>
> On Mon, Mar 11, 2013 at 3:35 PM, Jinyan Huang <jhu...@hsph.harvard.edu> wrote:
>> Dear list,
>>
>> I am enjoying to use ggplot2.
>>
>> But sometimes I found it is a bit of confused.
>>
>> For example, I just want to draw a segment. I use geom_segment.
>>
>> How can I know what kind of "linetype" I can use? Are there a full
>> list for "linetype" ?
>
> ?aes says
>
> "See Also:
>
> 'aes_string' for passing quoted variable names.
> 'aes_colour_fill_alpha', 'aes_group_order',
> 'aes_linetype_size_shape' and 'aes_position' for more specific
> examples with different aesthetics."
>
> ?aes_linetype_size_shape says
>
> " # Line types should be specified with either an integer, a name,
> or with a string of
> # an even number (up to eight) of hexidecimal digits which give
> the lengths in
> # consecutive positions in the string.
> # 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 =
> longdash, 6 = twodash"
>
>>
>> Why this is not working?
>>
>> geom_segment(aes(x = 10, y = 0, xend = 10, yend =
>> 300,col="red",linetype="dashed"))
>
> aes is for mapping variables to aesthetics. "dashed" is not a
> variable, but a constant. In this case you should set it outside of
> the aes call, as you did below.
>
> HTH,
> Ista
>
>>
>> But this is OK.
>> geom_segment(aes(x = 10, y = 0, xend = 10, yend =
>> 300,col="red"),linetype="dashed")
>>
>> Thank you.
>>
>> --
>> --
>> You received this message because you are subscribed to the ggplot2 mailing list.
>> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>>
>> To post: email ggp...@googlegroups.com
>> To unsubscribe: email ggplot2+u...@googlegroups.com
>> More options: http://groups.google.com/group/ggplot2
>>
>> ---
>> You received this message because you are subscribed to the Google Groups "ggplot2" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>



--
Best wishes,

Jinyan HUANG

Jinyan Huang

unread,
Mar 11, 2013, 4:05:40 PM3/11/13
to Ista Zahn, ggp...@googlegroups.com
geom_point(alpha = 0.5)
and
geom_point(aes(alpha = 0.5))
are different?

On Mon, Mar 11, 2013 at 3:43 PM, Ista Zahn <ista...@gmail.com> wrote:

Ista Zahn

unread,
Mar 11, 2013, 4:18:26 PM3/11/13
to Jinyan Huang, ggp...@googlegroups.com
Hi,

On Mon, Mar 11, 2013 at 4:02 PM, Jinyan Huang <jhu...@hsph.harvard.edu> wrote:
> Ista,
>
> Thank you for your explain. Your first part answers make sense to me.
> Later part still confused me.
>
> geom_segment(aes(x = 10, y = 0, xend = 10, yend =
> 300,col="red",linetype="dashed"))
>
> aes is for mapping variables to aesthetics. But here 10, 0, 10, 300,
> "red" (x = 10, y = 0, xend = 10, yend = 300,col="red") should also be
> constant. How can I understand this trick?

Ah, well, I guess the intuition doesn't always hold. But the
equivalent to your example is

ggplot() +
geom_line(aes(x = c(10, 10),
y = c(0, 300)),
col="red",
linetype="dashed")

in which y is a variable. Also it appears that position aesthetic
mappings a somewhat different than other mappings, in that

geom_line(x= 10, aes(y = c(0, 300)))

is not valid. So there may be some corner case exceptions to the rule.
I wouldn't get too hung up on that if I were you: as a general mental
model "aesthetic mapping=inside aes(), setting fixed values=outside
aes()" works most of the time.

Best,
Ista

Ista Zahn

unread,
Mar 11, 2013, 4:20:43 PM3/11/13
to Jinyan Huang, ggp...@googlegroups.com
On Mon, Mar 11, 2013 at 4:05 PM, Jinyan Huang <jhu...@hsph.harvard.edu> wrote:
> geom_point(alpha = 0.5)
> and
> geom_point(aes(alpha = 0.5))
> are different?


Yes, the first one sets alpha to .05. The second one creates a new
variable named 0.5, the value of which is 0.5, and maps alpha to it
(this is basically never what you want).

Louis Reynolds

unread,
Mar 11, 2013, 5:17:05 PM3/11/13
to ggp...@googlegroups.com
Hi,

I was having problems loading ggplot2 today so decided to download new R...did that (also downloaded new R studio).

Much later I am getting the same error..

Something to do with stringr?

Other packages (e.g., plyr) are working fine...(and everything worked fine last week).

install.packages("ggplot2")
Installing package(s) into 'C:/Program Files/R/R-2.15.3/library'
(as 'lib' is unspecified)
trying URL 'http://lib.stat.cmu.edu/R/CRAN/bin/windows/contrib/2.15/ggplot2_0.9.3.1.zip'
Content type 'application/zip' length 2665536 bytes (2.5 Mb)
opened URL
downloaded 2.5 Mb

package 'ggplot2' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
       C:\Documents and Settings\LReynold\Local Settings\Temp\Rtmp8MVtHM\downloaded_packages
library("ggplot2", lib.loc="C:/Program Files/R/R-2.15.3/library")
Error in loadNamespace(i, c(lib.loc, .libPaths())) :
 there is no package called 'stringr'
Error: package/namespace load failed for 'ggplot2'

Thank you.


Louis Reynolds

unread,
Mar 11, 2013, 9:20:32 PM3/11/13
to Dennis Murphy, ggp...@googlegroups.com
Dennis, thank you, you were right, I did need all of those other packages.

After some further searching I found that it was my antivirus software that kept those packages from installing. After disabled the software - everything loaded and works.


On Mar 11, 2013, at 5:36 PM, Dennis Murphy <djm...@gmail.com> wrote:

> stringr is one of Hadley's packages and should be installed, as parts
> of the ggplot2 code use it (plyr, reshape2, scales and gtable should
> also be installed [at a minimum]).
>
> Dennis

Jinyan Huang

unread,
Mar 12, 2013, 2:30:40 PM3/12/13
to Ista Zahn, ggp...@googlegroups.com
Ista,

Thank you. Your answer make sense to me.

On Mon, Mar 11, 2013 at 4:18 PM, Ista Zahn <ista...@gmail.com> wrote:
> Hi,
>
> On Mon, Mar 11, 2013 at 4:02 PM, Jinyan Huang <jhu...@hsph.harvard.edu> wrote:
>> Ista,
>>
>> Thank you for your explain. Your first part answers make sense to me.
>> Later part still confused me.
>>
>> geom_segment(aes(x = 10, y = 0, xend = 10, yend =
>> 300,col="red",linetype="dashed"))
>>
>> aes is for mapping variables to aesthetics. But here 10, 0, 10, 300,
>> "red" (x = 10, y = 0, xend = 10, yend = 300,col="red") should also be
>> constant. How can I understand this trick?
>
> Ah, well, I guess the intuition doesn't always hold. But the
> equivalent to your example is
>
> ggplot() +
> geom_line(aes(x = c(10, 10),
> y = c(0, 300)),
> col="red",
> linetype="dashed")
>
> in which y is a variable. Also it appears that position aesthetic
> mappings a somewhat different than other mappings, in that
>
> geom_line(x= 10, aes(y = c(0, 300)))
>
> is not valid. So there may be some corner case exceptions to the rule.
> I wouldn't get too hung up on that if I were you: as a general mental
> model "aesthetic mapping=inside aes(), setting fixed values=outside
> aes()" works most of the time.
>
> Best,
> Ista
>
>>
Reply all
Reply to author
Forward
0 new messages