Asked  7 Months ago    Answers:  5   Viewed   53 times

I have the following dataframe:

uniq <- structure(list(year = c(1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L), uniq.loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("u.1", "u.2", "u.3"), class = "factor"), uniq.n = c(1, 1, 1, 2, 5, 4, 2, 16, 16, 10, 15, 14, 8, 12, 20, 11, 17, 30, 17, 21, 22, 19, 34, 44, 56, 11, 0, 0, 3, 3, 7, 17, 12, 21, 18, 10, 12, 9, 7, 11, 25, 14, 11, 17, 12, 24, 59, 17, 36, 50, 59, 12, 0, 0, 0, 1, 4, 6, 3, 3, 9, 3, 4, 2, 5, 2, 12, 6, 8, 8, 3, 2, 9, 5, 20, 7, 10, 8), uniq.p = c(100, 100, 25, 33.3, 31.2, 14.8, 11.8, 40, 37.2, 43.5, 48.4, 56, 40, 48, 35.1, 35.5, 47.2, 54.5, 53.1, 44.7, 24.4, 46.3, 37.8, 43.6, 44.8, 35.5, 0, 0, 75, 50, 43.8, 63, 70.6, 52.5, 41.9, 43.5, 38.7, 36, 35, 44, 43.9, 45.2, 30.6, 30.9, 37.5, 51.1, 65.6, 41.5, 40, 49.5, 47.2, 38.7, 0, 0, 0, 16.7, 25, 22.2, 17.6, 7.5, 20.9, 13, 12.9, 8, 25, 8, 21.1, 19.4, 22.2, 14.5, 9.4, 4.3, 10, 12.2, 22.2, 6.9, 8, 25.8)), .Names = c("year", "uniq.loc", "uniq.n", "uniq.p"), class = "data.frame", row.names = c(NA, -78L))

When I make an area-plot with:

ggplot(data = uniq) + 
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
  scale_x_continuous(limits=c(1986,2014)) +
  scale_y_continuous(limits=c(0,101)) +
  theme_bw()

I get this result:

enter image description here

However, I want to remove the space between the axis and the actual plot. When I add theme(panel.grid = element_blank(), panel.margin = unit(-0.8, "lines")) I get the following error message:

Error in theme(panel.grid = element_blank(), panel.margin = unit(-0.8,  : 
  could not find function "unit"

Any suggestions on how to solve this problem?

 Answers

46

Update: See @divibisan's answer for further possibilities in the latest versions of ggplot2.


From ?scale_x_continuous about the expand-argument:

Vector of range expansion constants used to add some padding around the data, to ensure that they are placed some distance away from the axes. The defaults are to expand the scale by 5% on each side for continuous variables, and by 0.6 units on each side for discrete variables.

The problem is thus solved by adding expand = c(0,0) to scale_x_continuous and scale_y_continuous. This also removes the need for adding the panel.margin parameter.

The code:

ggplot(data = uniq) + 
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
  scale_x_continuous(limits = c(1986,2014), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0,101), expand = c(0, 0)) +
  theme_bw() +
  theme(panel.grid = element_blank(),
        panel.border = element_blank())

The result: plot area chart with no gaps

Tuesday, June 1, 2021
 
nika
answered 7 Months ago
97

Just following up on what Ian suggested: for ggplot2 you really want all the y-axis stuff in one column with another column as a factor indicating how you want to decorate it. It is easy to do this with melt. To wit:

qplot(x_value, value, 
      data = melt(dd, measure.vars=c("Predicted_value", "Actual_value")), 
      colour=variable) + facet_wrap(~State_CD)

Here's what it looks like for me: alt text
(source: princeton.edu)

To get an idea of what melt is actually doing, here's the head:

> head(melt(dd, measure.vars=c("Predicted_value", "Actual_value")))
     x_value State_CD        variable      value
1  1.2898779        A Predicted_value  1.0913712
2  0.1077710        A Predicted_value -2.2337188
3 -0.9430190        A Predicted_value  1.1409515
4  0.3698614        A Predicted_value -1.8260033
5 -0.3949606        A Predicted_value -0.3102753
6 -0.1275037        A Predicted_value -1.2945864

You see, it "melts" Predicted_value and Actual_value into one column called value and adds another column called variable letting you know what column it originally came from.

Sunday, August 8, 2021
 
michele
answered 4 Months ago
96

Is this what you mean?

p_all <- ggplot(AB, aes(x=prime,y=RT,group=interaction(familiarity,block))) +
  geom_line(aes(linetype=familiarity,color=block)) 

enter image description here

Data used:

AB <- structure(list(block = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L), .Label = c("A", "B"), class = "factor"), familiarity = structure(c(1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L), class = "factor", .Label = c("fam", 
"unfam")), prime = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), class = "factor", .Label = c("P", "UP")), RT = c(570.6929, 
628.7446, 644.6268, 607.4312, 556.3581, 645.4821, 623.5624, 604.4113
)), .Names = c("block", "familiarity", "prime", "RT"), row.names = c(NA, 
-8L), class = "data.frame")
Saturday, August 28, 2021
 
kat_indo
answered 4 Months ago
25

In order to achieve this you need to re-template the chart. The standard chart template is as follows:

            <ControlTemplate TargetType="charting:Chart">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />

                        <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                        <Grid Grid.Row="1" Margin="0,15,0,15">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <datavis:Legend x:Name="Legend" Title="{TemplateBinding LegendTitle}" Style="{TemplateBinding LegendStyle}" Grid.Column="1" />
                            <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                                <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                            </chartingprimitives:EdgePanel>
                        </Grid>
                    </Grid>
                </Border>
            </ControlTemplate>

This details the location of the plot area, title, legend etc... It also included a hard-coded margin around the plot area, so you cannot achieve what you are after by simply styling the chart. If you just want a chart area and nothing else, you can simplify the chart template as follows:

xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"

<Grid>
  <chartingToolkit:Chart x:Name="chart" Width="500" Height="300"
                         Margin="0, 0, 0, 0" Padding="0">
    <chartingToolkit:AreaSeries ItemsSource="{Binding}"  
                                  DependentValuePath="Value"
                                  IndependentValuePath="Key"
                                  Background="Red">
    </chartingToolkit:AreaSeries>
    <chartingToolkit:Chart.Axes>
      <chartingToolkit:LinearAxis Orientation="X" ShowGridLines="False" Height="0">
      </chartingToolkit:LinearAxis>
      <chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="False" Width="0"/>
    </chartingToolkit:Chart.Axes>
    <chartingToolkit:Chart.Template>
      <ControlTemplate TargetType="chartingToolkit:Chart">
        <Border Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Padding="{TemplateBinding Padding}">
          <Grid>
            <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
              <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
              <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
            </chartingprimitives:EdgePanel>
          </Grid>
        </Border>
      </ControlTemplate>
    </chartingToolkit:Chart.Template>
  </chartingToolkit:Chart>
</Grid>

This will remove the padding that you are seeing.

Monday, September 27, 2021
 
Jason
answered 3 Months ago
84

I understand the spaces you need to remove reside in between letters.

Use

re.sub(r'(?<=[^Wd_])s+(?=[^Wd_])', '', text)

Details:

  • (?<=[^Wd_]) - a positive lookbehind requiring a Unicode letter immediately to the left of the current location
  • s+ - 1+ whitespaces (remove + if only one is expected)
  • (?=[^Wd_]) - a positive lookahead that requires a Unicode letter immediately to the right of the current location.

You do not need re.U flag since it is on by default in Python 3. You need it in Python 2 though.

You may also use capturing groups:

re.sub(r'([^Wd_])s+([^Wd_])', r'12', text)

where the non-consuming lookarounds are turned into consuming capturing groups ((...)). The 1 and 2 in the replacement pattern are backreferences to the capturing group values.

See a Python 3 online demo:

import re
text = "今天特别 热,但是我买了 3 个西瓜。"
print(re.sub(r'(?<=[^Wd_])s+(?=[^Wd_])', '', text))
// => 今天特别热,但是我买了 3 个西瓜。
Monday, November 22, 2021
 
MonsterMMORPG
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share